Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/795654a855dcda537ed0a7c593eb95b3 to your computer and use it in GitHub Desktop.
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/d7c88f72c3c5a5ebeab335bd2c6d77baee94858b
// 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.2.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