Skip to content

Instantly share code, notes, and snippets.

@krasserm
Created July 14, 2011 10:32
Show Gist options
  • Select an option

  • Save krasserm/1082242 to your computer and use it in GitHub Desktop.

Select an option

Save krasserm/1082242 to your computer and use it in GitHub Desktop.

Revisions

  1. krasserm revised this gist Jul 14, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MonadTransformerExamples.scala
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    import scalaz._
    import Scalaz._

    object ExampleMonadTransformer {
    object MonadTransformerExamples {
    def main(args: Array[String]) = run

    def run {
  2. krasserm revised this gist Jul 14, 2011. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions MonadTransformerExamples.scala
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    package scalaz.example

    import scalaz._
    import Scalaz._

  3. krasserm created this gist Jul 14, 2011.
    71 changes: 71 additions & 0 deletions MonadTransformerExamples.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    package scalaz.example

    import scalaz._
    import Scalaz._

    object ExampleMonadTransformer {
    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
    }
    }