Created
December 26, 2011 11:57
-
-
Save einblicker/1520982 to your computer and use it in GitHub Desktop.
resumption monad
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
object ResumptionMonad extends App { | |
sealed abstract class Resumption[A] { | |
def map[B](k: A => B) = flatMap(k andThen Val.apply) | |
def flatMap[B](k: A => Resumption[B]): Resumption[B] = { | |
def loop(x: Resumption[A]): Resumption[B] = | |
x match { | |
case Val(value) => k(value) | |
case Wrap(value) => Wrap(loop(value)) | |
} | |
loop(this) | |
} | |
} | |
case class Val[A](value: A) extends Resumption[A] | |
case class Wrap[A](value: Resumption[A]) extends Resumption[A] | |
val Wrap(Wrap(Wrap(Val(a)))) = for { | |
x <- Wrap(Wrap(Val(1))) | |
y <- Wrap(Val(2)) | |
} yield x + y | |
println(a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment