Richard Searle's Blog

Thoughts about software

Archive for the ‘scalaz’ Category

scalaz contrib and Futures

Posted by eggsearle on July 24, 2013

The canonical combined Future example is

def c(i:Int):Future[Int] = ...
val f1 = c(5)
val f2 = c(2)
for{
i1 <- f1
i2 <- f2
}
yield i1+i2

Creating the Futures within the for-comprehension would be much tidier, but then they would execute sequentially ruining the entire purpose.

The scalaz contrib project provides support for Scala 2.10 futures, allowing more expressive coding for some tasks.

This applicative functor expression is equivalent to the above code.

val r:Future[Int] = (c(5) |@| c(1))(_ + _)

 

Advertisements

Posted in Scala, scalaz | Leave a Comment »