Skip to content

Instantly share code, notes, and snippets.

@arouel
Last active August 29, 2015 14:02
Show Gist options
  • Save arouel/357519c9a0e274505f47 to your computer and use it in GitHub Desktop.
Save arouel/357519c9a0e274505f47 to your computer and use it in GitHub Desktop.
Scalaz's Tasks can be composed easily
package com.github.before.examples
import scalaz.concurrent._
/**
* Example how to compose Scalaz Tasks with a For-Comprehension
*/
object TaskCompositionDemo {
// first task
val t1 = Task.delay(List(1, 2, 3, 4, 5)) //> t1 : scalaz.concurrent.Task[List[Int]] = scalaz.concurrent.Task@5d3411d
// second task
val t2 = Task.delay(List(6, 7, 8, 9, 0)) //> t2 : scalaz.concurrent.Task[List[Int]] = scalaz.concurrent.Task@3bfdc050
// sequence tasks
val t3 = for {
r1 <- t1
r2 <- t2
} yield r1 ++ r2 //> t3 : scalaz.concurrent.Task[List[Int]] = scalaz.concurrent.Task@685cb137
// run first and second task within `t3`
t3.run //> res0: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment