public
Last active

Scalaz 7 monad transformer examples

  • Download Gist
MonadTransformerExamples.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
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
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.