Richard Searle's Blog

Thoughts about software

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: