Skip to content

Instantly share code, notes, and snippets.

@alexandru
Last active March 3, 2016 08:32
Show Gist options
  • Select an option

  • Save alexandru/fed4a9ee963c7440b39d to your computer and use it in GitHub Desktop.

Select an option

Save alexandru/fed4a9ee963c7440b39d to your computer and use it in GitHub Desktop.
import monix.execution._
import monix.execution.RunLoop.FrameId
def fibbonacciWithMacros(iterations: Int, callback: BigInt => Unit)
(implicit s: Scheduler): Unit = {
def loop(idx: Int, a: BigInt, b: BigInt, frameId: FrameId): Unit =
if (idx >= iterations) callback(b) else
RunLoop.step(frameId) { frameId => loop(idx+1, b, a+b, frameId) }
RunLoop.start(frameId => loop(0, BigInt(0), BigInt(1), frameId))
}
def fibbonacciSimple(iterations: Int, callback: BigInt => Unit)
(implicit s: Scheduler): Unit = {
def loop(idx: Int, a: BigInt, b: BigInt, frameId: Int): Unit =
if (idx >= iterations) callback(b) else {
val nextFrameId = (frameId + 1) & s.batchedExecutionModulus
if (nextFrameId > 0)
loop(idx+1, b, a+b, nextFrameId)
else // call-stack full, inserting asynchronous boundary
s.execute(new Runnable {
def run() = loop(idx+1, b, a+b, nextFrameId)
})
}
// Starting the loop
if (s.batchedExecutionModulus == 0) // execution always async?
s.execute(new Runnable { def run() = loop(0, BigInt(0), BigInt(0), 0) })
else
loop(0, BigInt(0), BigInt(1), 0)
}
// To play with these ...
import monix.execution.Scheduler.Implicits.global
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment