Skip to content

Instantly share code, notes, and snippets.

@clayrat
Last active December 21, 2015 22:39
Show Gist options
  • Save clayrat/6376562 to your computer and use it in GitHub Desktop.
Save clayrat/6376562 to your computer and use it in GitHub Desktop.
case class Global[A](a: A, state: Int) {
def map[B](fun: A => B): Global[B] = Global(fun(a), state)
def flatMap[B](fun: A => Global[B]): Global[B] = fun(a)
override def toString = a.toString + " " + state
}
object Global {
def point[A](a: A): Global[A] = Global(a, 0)
}
object monadTupo {
var global = 0
def a(x: String): Double = { global = 1; x.toDouble }
def b(y: Double): Int = { global = 2; y.toInt }
def fa(x: String): Global[Double] = Global(x.toDouble, 1)
def fb(y: Double): Global[Int] = Global(y.toInt, 2)
def main(args: Array[String]) {
println((a _ andThen b _)("1.1"))
println(global)
import Global._
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