Skip to content

Instantly share code, notes, and snippets.

@tixxit
Created April 1, 2014 17:11
Show Gist options
  • Save tixxit/9918532 to your computer and use it in GitHub Desktop.
Save tixxit/9918532 to your computer and use it in GitHub Desktop.
import scalaz._
import scala.util.Try
final class X {
val magic: Int = scala.util.Random.nextInt(10000)
def isStable: Boolean = magic == 42
}
object Main extends App {
def startStack(x: X): X = {
val StateX = StateT.stateMonad[X]
import StateX._ // includes whileM_, gets, put
whileM_(gets(!_.isStable), modify(_ => new X)).exec(x)
}
def startHeap(x: X): X = {
val StateX = StateT.stateTMonadState[X, Free.Trampoline]
import StateX._ // includes whileM_, gets, put
whileM_(gets(!_.isStable), modify(_ => new X)).exec(x).run
}
println(s"heap = ${startHeap(new X).magic}")
println(s"stack = ${Try(startStack(new X).magic).getOrElse("Overflow!")}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment