Forked from channingwalton/MonadTransformerExamples.scala
Created
August 10, 2013 20:43
-
-
Save larsrh/6202061 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ------------------------------------------------------ | |
// 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