Created
August 29, 2013 10:59
-
-
Save clayrat/6376717 to your computer and use it in GitHub Desktop.
This file contains 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
case class TupoIO[A](a: A, out: String) { | |
def map[B](fun: A => B): TupoIO[B] = TupoIO(fun(a), out) | |
def flatMap[B](fun: A => TupoIO[B]): TupoIO[B] = { | |
val res = fun(a) | |
TupoIO(res.a, out + res.out) | |
} | |
override def toString = out + a.toString | |
} | |
object TupoIO { | |
def point[A](a: A): TupoIO[A] = TupoIO(a, "") | |
} | |
object monadTupoIO { | |
def a(x: String): Double = { println("in A"); x.toDouble } | |
def b(y: Double): Int = { println("in B"); y.toInt + 2 } | |
def fa(x: String): TupoIO[Double] = TupoIO(x.toDouble, "in A\n") | |
def fb(y: Double): TupoIO[Int] = TupoIO(y.toInt + 2, "in B\n") | |
def main(args: Array[String]) { | |
println((a _ andThen b _)("1.1")) | |
import TupoIO._ | |
println(point("1.1").flatMap(fa).flatMap(fb)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment