Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save channingwalton/6197556 to your computer and use it in GitHub Desktop.
Save channingwalton/6197556 to your computer and use it in GitHub Desktop.
// ------------------------------------------------------
// 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