Last active
December 21, 2015 22:39
-
-
Save clayrat/6376562 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 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