Skip to content

Instantly share code, notes, and snippets.

@mathhun
Last active July 11, 2016 15:55
Show Gist options
  • Save mathhun/af2f89deda474223caf4a766752d4a75 to your computer and use it in GitHub Desktop.
Save mathhun/af2f89deda474223caf4a766752d4a75 to your computer and use it in GitHub Desktop.
EitherT[Future, String, _]
import com.github.nscala_time.time.Imports._
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scalaz._, syntax.either._, std.AllInstances._
object Main extends App {
type EitherFuture[A] = EitherT[Future, String, A]
def date(): String = DateTimeFormat.forPattern("HH:mm:ss").print(new DateTime)
def efx(): EitherFuture[String] = {
EitherT {
Future {
println("-> x: " ++ date)
Thread.sleep(5000)
println("<- x: " ++ date)
"x".right
//"x-failed".left
}
}
}
def efy(): EitherFuture[String] = {
EitherT {
Future {
println("-> y: " ++ date)
Thread.sleep(3000)
println("<- y: " ++ date)
"y".right
}
}
}
println("start: " ++ date)
val c0 = Future {
println("-> c0: " ++ date)
Thread.sleep(5000)
println("<- c0: " ++ date)
}
val r: EitherFuture[String] = for {
x <- efx
y <- efy
} yield x ++ y
Await.result(r.run, Duration.Inf) match {
case \/-(x) => println("success: " ++ x)
case -\/(e) => println("failure: " ++ e)
}
Await.result(c0, Duration.Inf)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment