Last active
March 3, 2016 08:32
-
-
Save alexandru/fed4a9ee963c7440b39d to your computer and use it in GitHub Desktop.
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
| 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