Skip to content

Instantly share code, notes, and snippets.

@clayrat
Created August 29, 2013 10:59
Show Gist options
  • Save clayrat/6376717 to your computer and use it in GitHub Desktop.
Save clayrat/6376717 to your computer and use it in GitHub Desktop.
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