Skip to content

Instantly share code, notes, and snippets.

@gigiigig
Created October 8, 2014 12:22
Show Gist options
  • Save gigiigig/e0d2c0314fe03022b1e0 to your computer and use it in GitHub Desktop.
Save gigiigig/e0d2c0314fe03022b1e0 to your computer and use it in GitHub Desktop.
Transformer
import scala.concurrent.Future
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scalaz._
import Scalaz._
object Console extends App {
val v1: Future[Option[Int]] = 1.point[Option].point[Future]
val v2: Future[Option[Int]] = 2.point[Option].point[Future]
val foo1: Future[Option[Future[Option[Int]]]] = v1.map(_.map { v1 =>
v2.map(_.map { v2 =>
v1 + v2
})
})
val foo2: Future[Option[Int]] = v1.map(_.map { v1 =>
v2.map(_.map { v2 =>
v1 + v2
})
}.sequence.map(_.flatten)).flatMap(identity)
val foo3: Future[Option[Int]] = v1.map(_.traverse { v1 =>
v2.map(_.map { v2 =>
v1 + v2
})
}.map(_.flatten)).flatMap(identity)
val foo4: Future[Option[Int]] = (for {
v1 <- OptionT[Future, Int](v1)
v2 <- OptionT(v2)
} yield (v1 + v2)).run
val v3: Option[Int] = 3.point[Option]
val v4: Future[Int] = 4.point[Future]
val foo5: Future[Option[Int]] = (for {
v1 <- OptionT(v1)
v2 <- OptionT(v2)
v3 <- OptionT(v3.point[Future])
v4 <- v4.liftM[OptionT]
} yield (v1 + v2 + v3 + v4)).run
require(Await.result(foo5, 2 seconds).get == 10)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment