Created
July 14, 2011 10:32
-
-
Save krasserm/1082242 to your computer and use it in GitHub Desktop.
Scalaz 7 monad transformer examples
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
import scalaz._ | |
import Scalaz._ | |
object MonadTransformerExamples { | |
def main(args: Array[String]) = run | |
def run { | |
// ------------------------------------------------------ | |
// Combined Option/Option | |
// ------------------------------------------------------ | |
val a1: OptionT[Option, String] = some("a1").transLift[OptionT] | |
val a2: OptionT[Option, String] = optionT(some(some("a2"))) | |
val a3: OptionT[Option, String] = optionT(some(none)) | |
(a1 ∘ (_ + "x")).runT assert_=== Some(Some("a1x")) | |
(a2 ∘ (_ + "x")).runT assert_=== Some(Some("a2x")) | |
(a3 ∘ (_ + "x")).runT assert_=== Some(None) | |
(for (x <- a1) yield x + "y").runT assert_=== Some(Some("a1y")) | |
// ------------------------------------------------------ | |
// Combined List/Option | |
// ------------------------------------------------------ | |
val b1: OptionT[List, String] = List("b1a", "b1b").transLift[OptionT] | |
val b2: OptionT[List, String] = optionT(List(some("b2a"), some("b2b"))) | |
val b3: OptionT[List, String] = optionT(List(some("b3"), none)) | |
(b1 ∘ (_ + "x")).runT assert_=== List(Some("b1ax"), Some("b1bx")) | |
(b2 ∘ (_ + "x")).runT assert_=== List(Some("b2ax"), Some("b2bx")) | |
implicit def ListEach: Each[List] = new Each[List] { | |
def each[A](f: A => Unit) = _ foreach f | |
} | |
for { | |
x <- b2 | |
y <- b3 | |
} print("%s " format (x + y)) // b2ab3 b2bb3 | |
println | |
// ------------------------------------------------------ | |
// Combined List/Option/Either | |
// ------------------------------------------------------ | |
type EitherTString[α[_], β] = ({type λ[α[_], β] = EitherT[String, α, β]})#λ[α, β] | |
type OptionTList [α] = ({type λ[α] = OptionT[List, α] })#λ[α] | |
val c1: EitherT[String, OptionTList, String] = List("c1a", "c1b").transLift[OptionT].transLift[EitherTString] | |
val c2: EitherT[String, OptionTList, String] | |
= eitherT[String, OptionTList, String](optionT[List, Either[String, String]](List(some(Right("c2a")), some(Right("c2b"))))) | |
val c3: EitherT[String, OptionTList, String] | |
= eitherT[String, OptionTList, String](optionT[List, Either[String, String]](List(some(Right("c3")), none, some(Left("error"))))) | |
(c1 ∘ (_ + "x")).runT.runT assert_=== List(some(Right("c1ax")), some(Right("c1bx"))) | |
(c2 ∘ (_ + "x")).runT.runT assert_=== List(some(Right("c2ax")), some(Right("c2bx"))) | |
implicit def OptionTListEach: Each[OptionTList] = new Each[OptionTList] { | |
def each[A](f: A => Unit) = _ foreach f | |
} | |
for { | |
x <- c2 | |
y <- c3 | |
} print("%s " format (x + y)) // c2ac3 c2bc3 | |
println | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment