Last active
May 25, 2024 10:18
-
-
Save dacr/795654a855dcda537ed0a7c593eb95b3 to your computer and use it in GitHub Desktop.
ZIO learning - operations timeout with fibonacci compute / published by https://github.com/dacr/code-examples-manager #e30c9102-80ff-449f-8c47-8e6ba7e82ca1/8bec4f9ed60d4937b56a3fda0eff93eeb137d973
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
// summary : ZIO learning - operations timeout with fibonacci compute | |
// keywords : scala, zio, learning, pure-functional, fibonacci | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : e30c9102-80ff-449f-8c47-8e6ba7e82ca1 | |
// created-on : 2021-04-06T12:48:09+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0" | |
// --------------------- | |
/* | |
inspired from | |
- [John A De Goes - ZIO: Next-Generation Effects in Scala](https://www.youtube.com/watch?v=mkSHhsJXjdc) | |
- +31:15 | |
with fixes due to API changes | |
------ | |
interesting content also here : https://gist.github.com/jdegoes/a799c7365d877da1face69dd139466de | |
*/ | |
import zio.*, zio.worksheet.* | |
import zio.Duration.* | |
def fib(n: BigInt): UIO[BigInt] = { | |
if (n <= 1) ZIO.succeed(n) | |
//else fib(n-2).zipWithPar(fib(n-1))(_ + _) // TODO - some memory issue here => to be continued... linked with too many fibers I guess... | |
else fib(n - 2).zipWith(fib(n - 1))(_ + _) | |
} | |
val maybeBigFib = fib(40).timeout(10.seconds) | |
//val maybeBigFib = fib(50).timeout(5.minutes) | |
//val maybeBigFib = fib(100_000).timeout(5.minutes) | |
// ------------------------------------------------------------- | |
val result = maybeBigFib.unsafeRun | |
println(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment