Skip to content

Instantly share code, notes, and snippets.

@ceedubs
Created January 24, 2014 00:22
Show Gist options
  • Save ceedubs/8589661 to your computer and use it in GitHub Desktop.
Save ceedubs/8589661 to your computer and use it in GitHub Desktop.
Defining type alias ErrorsOrT to use liftM on EitherT instances without using a type lambda
scala> import scalaz.syntax.id._ // for .right
import scalaz.syntax.id._
scala> import scalaz.EitherT
import scalaz.EitherT
scala> import scalaz.NonEmptyList
import scalaz.NonEmptyList
scala> import scala.concurrent.Future
import scala.concurrent.Future
scala> import scalaz.syntax.monad._ // for liftM
import scalaz.syntax.monad._
scala> import scalaz.contrib.std.scalaFuture._ // typeclass instances for Scala Future
import scalaz.contrib.std.scalaFuture._
scala> import ExecutionContext.Implicits.global // implicit execution context...you probably want a better one for real-world
import ExecutionContext.Implicits.global
scala> type ErrorsOrT[M[+_], +A] = EitherT[M, NonEmptyList[String], A] // String could be your error type of choice
defined type alias ErrorsOrT
scala> :paste
// Entering paste mode (ctrl-D to finish)
for {
thing1 <- EitherT(Future.successful(1.right))
thing2 <- Future.successful(2).liftM[ErrorsOrT]
} yield thing1 + thing2
// Exiting paste mode, now interpreting.
res7: scalaz.EitherT[scala.concurrent.Future,scalaz.NonEmptyList[String],Int] = scalaz.EitherTFunctions$$anon$11@45d1a86c
@anowak
Copy link

anowak commented Mar 9, 2016

Lifting to Future[Error \/ A] to EitherT[Future, Error, A] can be accomplished with EitherT.eitherT().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment