Skip to content

Instantly share code, notes, and snippets.

@dakatsuka
Created June 15, 2018 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dakatsuka/1cd2c2ddcceb56f51760b32136ed23cd to your computer and use it in GitHub Desktop.
Save dakatsuka/1cd2c2ddcceb56f51760b32136ed23cd to your computer and use it in GitHub Desktop.
import scalaz._
import Scalaz._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
object Main extends App {
val getEitherTFutures1: Seq[EitherT[Future, Throwable, String]] = {
Seq(
EitherT[Future, Throwable, String](Future.apply(\/-("hoge"))),
EitherT[Future, Throwable, String](Future.apply(\/-("hoge"))),
EitherT[Future, Throwable, String](Future.apply(\/-("hoge")))
)
}
val getEitherTFutures2: Seq[EitherT[Future, Throwable, String]] = {
Seq(
EitherT[Future, Throwable, String](Future.apply(\/-("hoge"))),
EitherT[Future, Throwable, String](Future.apply(\/-("hoge"))),
EitherT[Future, Throwable, String](Future.apply(-\/(new Exception)))
)
}
val res1 = getEitherTFutures1.toList.sequenceU // EitherT[Future, Throwable, List[String]]
val res2 = getEitherTFutures2.toList.sequenceU // EitherT[Future, Throwable, List[String]]
Await.result(res1.run.map(_.map(println)), Duration.Inf) // List(hoge, hoge, hoge)
Await.result(res2.run.map(e => println(e.left)), Duration.Inf) // -\/(-\/(java.lang.Exception))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment