Created
March 17, 2014 21:01
-
-
Save davidpeklak/9608270 to your computer and use it in GitHub Desktop.
EitherTWriterT
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._ | |
object WE { | |
type EA[+A] = (String \/ A) | |
type WT[+A] = WriterT[EA, Seq[Any], A] | |
def replace(j: Int)(i: Int): (String \/ Int) = { | |
if (j > 9) -\/("Failed with " + j.toString) | |
else \/-(j) | |
} | |
val init = \/-(5) | |
val r1 = init.flatMap(replace(7)).flatMap(replace(10)).flatMap(replace(4)) | |
val initNix: (Seq[Any], EA[Int]) = Seq() -> init | |
type WA[+A] = WriterT[Id.Id, Seq[Any], A] | |
def WA[A](t: (Seq[Any], A)) = WriterT[Id.Id, Seq[Any], A](t) | |
val initW = WA(initNix) | |
type EWA[+A] = EitherT[WA, String, A] | |
def EWA[A](wa: WA[EA[A]]) = EitherT[WA, String, A](wa) | |
val initEW = EWA(initW) | |
def reWe(j: Int)(i: Int): EWA[Int] = EWA(WA(replace(j)(i).fold(f => Seq(f) -> -\/(f), v => Seq[Any](v) -> \/-(v)))) | |
implicit val seqMonoid: Monoid[Seq[Any]] = new Monoid[Seq[Any]] { | |
def append(f1: Seq[Any], f2: => Seq[Any]): Seq[Any] = f1 ++ f2 | |
def zero: Seq[Any] = Seq() | |
} | |
implicit val WaMonad: Monad[WA] = WriterT.writerTMonad[Id.Id, Seq[Any]] | |
val r = initEW.flatMap(reWe(7)).flatMap(reWe(8)).flatMap(reWe(10)).flatMap(reWe(4)) | |
val result = r.run.run | |
} |
gseitz
commented
Mar 18, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment