Forked from krasserm/MonadTransformerExamples.scala
Last active
December 20, 2015 21:29
-
-
Save channingwalton/6197556 to your computer and use it in GitHub Desktop.
This file contains 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._, Scalaz._ | |
import EitherT._ | |
type OptionTList[α] = ({ type λ[α] = OptionT[List, α] })#λ[α] | |
val c1: EitherT[OptionTList, String, String] = eitherT[OptionTList, String, String](OptionT[List, \/[String, String]](List(some(\/-("c1a")), some(\/-("c1b"))))) | |
val c2: EitherT[OptionTList, String, String] = eitherT[OptionTList, String, String](OptionT[List, \/[String, String]](List(some(\/-("c2a")), some(\/-("c2b"))))) | |
val c3: EitherT[OptionTList, String, String] = eitherT[OptionTList, String, String](OptionT[List, \/[String, String]](List(some(\/-("c3")), none, some(-\/("error"))))) | |
// ∘ = map | |
(c1 ∘ (_ + "x")).run.run assert_=== List(some(\/-("c1ax")), some(\/-("c1bx"))) | |
(c2 ∘ (_ + "x")).run.run assert_=== List(some(\/-("c2ax")), some(\/-("c2bx"))) | |
(c3 ∘ (_ + "x")).run.run assert_=== List(some(\/-("c3x")), none, some(-\/("error"))) | |
for { | |
x ← c2 | |
y ← c3 | |
} yield println("%s " format (x + y)) // c2ac3 c2bc3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment