Coalesce data functionally

In a recent project I had to coalesce quite significant amount of data in the following way. To simplify it for this post, consider that we have the following two lists.

val x = List(“a”, “b”, “c”, “a”)

val y = List(1, 2, 6, 9)

We are about to write a function which would return the following list as the result.

val result = List((a,10), (b,2), (c,6))

Basically it would coalesce value with the same category. See for instance “b” in the above example.

Language that came up with repl inherently provides very nice way to try out different expression and to get to the expected outcome. In this context, as we are using scala, we can use repl-driven development quite conveniently as illustrated below.

  • Define the Lists:
scala> val x = List("a", "b" , "c", "a")
x: List[String] = List(a, b, c, a)

scala> val y = List(1,2,6,9)
y: List[Int] = List(1, 2, 6, 9)
  • Zip them.
scala> val z = x zip y
z: List[(String, Int)] = List((a,1), (b,2), (c,6), (a,9))
scala>
  • Group them based on the values of x.
scala> val grps = z groupBy (_._1)
grps: scala.collection.immutable.Map[String,List[(String, Int)]] = Map(b -> List((b,2)), a -> List((a,1), (a,9)), c -> List((c,6)))

scala>
  • Map the values of res8 and reduce them to compute the sum.
scala> val res = grps.values.map {_.reduce((i,j) => (i._1, (i._2+j._2)))}

res: Iterable[(String, Int)] = List((b,2), (a,10), (c,6))
scala>
  • Sort res based on the 1st value of the tuple.
scala> res.toList.sorted
res23: List[(String, Int)] = List((a,10), (b,2), (c,6))

scala>

Thus, the function can be simply written as follows:

def coalesce(x:List[String], y:List[Int]):List[(String, Int)] =
(x zip y).groupBy(_._1)
.values
.map{_.reduce((i,j) => (i._1, (i._2 + j._2)))}
.toList
.sorted
view raw coalesce.scala hosted with ❤ by GitHub

Thus we get the expected result.

Akka: Links, News And Resources (7)

Akka: Links, News And Resources (6)

Angel \"Java\" Lopez on Blog

Previous Post
Next Post

You know: I’m interested in actor models in general, and Akka implementation in particular, having distributed applications. I started two projects implementing Akka ideas, in Node.js and in C#:

https://github.com/ajlopez/SimpleActors
https://github.com/ajlopez/Aktores

Now, more links of my collection of links:

mrb: Distributed Systems Archaeology: Ricon West, 2013.10.30
http://michaelrbernste.in/2013/11/22/distributed-systems-archaeology.html

Using Akka in Scala IDE – Stack Overflow
http://stackoverflow.com/questions/13585927/using-akka-in-scala-ide

Pacific Northwest Scala 2013 Akka in Production: Our Story by Evan Chan – YouTube
http://www.youtube.com/watch?v=c1heorOM2LE

Akka vs Storm | Blog of Adam Warski | Planet JBoss Community
http://planet.jboss.org/post/akka_vs_storm

Akka in Production: Our Story
http://www.slideshare.net/EvanChan2/akka-inproductionpnw-scala2013

typesafe.com
http://typesafe.com/blog/typesafe-gets-sprayed

Typesafe Reactive Platform Acquires New High-Performance HTTP Foundation
http://www.marketwired.com/press-release/Typesafe-Reactive-Platform-Acquires-New-High-Performance-HTTP-Foundation-1841738.htm

Dev Time: Cope with Failure – Actor Supervision in Akka
http://blog.florian-hopf.de/2013/10/cope-with-failure-actor-supervision-in.html

Akka Work Pulling Pattern to prevent mailbox overflow, throttle and distribute work » Michael on development
http://www.michaelpollmeier.com/akka-work-pulling-pattern/

Going Reactive: Event-Driven, Scalable, Resilient & Responsive Systems
http://www.slideshare.net/jboner/going-reactive-eventdriven-scalable-resilient-systems

Let it crash • Where Akka Came From

View original post 18 more words

JAVA 8: Building a TriFunction functional interface

Java 8 facilitates several functional interface in java.util.function package. If we observe carefully, there are primarily four kinds of interfaces provided in this package, which are:

  • Supplier
  • Consumer
  • Function
  • Predicate

with the following signatures:

Supplier: () -> T
Consumer: T -> ()
Predicate: T -> boolean
Function: T -> R

In this post, alike java.util.function package, we define a new functional interface, called TriFrunction that accepts three arguments as parameters and returns the result after computation.

@FunctionalInterface
public interface TriFunction<T,U,S, R> {

/**
* Applies this function to the given arguments.
*
* @param t the first function argument
* @param u the second function argument
* @param s the third function argument
* @return the function result
*/
R apply(T t, U u, S s);
}

We can use this functional interface to compute the volume of a rectangular prism, by using following lambda expression.

TriFunction volume = (x,y,z) -> x*y*z

To use this lambda expression in the volume computation, we can simply use the following statement

volume.apply(2.4, 5.3, 10.4)

In this post, we have shown how to create a custom functional interface and use it to write concise lambda expressions using Java 8. It seems quite straightforward. If you have any question/remark, please leave a comment below.