Skip to content

Instantly share code, notes, and snippets.

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 larsrh/6202061 to your computer and use it in GitHub Desktop.
Save larsrh/6202061 to your computer and use it in GitHub Desktop.
// ------------------------------------------------------
// Combined List/Option/Either
// ------------------------------------------------------
object MonadTransformers extends App {
import scalaz._
import scalaz.std.option._
import scalaz.std.list._
type OptionTList[α] = OptionT[List, α]
val c1 = EitherT[OptionTList, String, String](OptionT[List, \/[String, String]](List(some(\/-("c1a")), some(\/-("c1b")))))
val c2 = EitherT[OptionTList, String, String](OptionT[List, \/[String, String]](List(some(\/-("c2a")), some(\/-("c2b")))))
val c3 = EitherT[OptionTList, String, String](OptionT(List(some(\/-("c3")), none, some(-\/("error")))))
assert((c1 map (_ + "x")).run.run == List(some(\/-("c1ax")), some(\/-("c1bx"))))
assert((c2 map (_ + "x")).run.run == List(some(\/-("c2ax")), some(\/-("c2bx"))))
assert((c3 map (_ + "x")).run.run == List(some(\/-("c3x")), none, some(-\/("error"))))
import scalaz.effect.IO
import scalaz.syntax.monad._
import scalaz.syntax.foldable._
import scalaz.syntax.effect.id._
val res = for {
x ← c2
y ← c3
} yield "%s " format (x + y)
val effect = res.traverse_(s => IO { print(s) }) >> IO { println }
effect.unsafePerformIO // c2ac3 c2bc3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment