Memoization with ZIO
ZIO tip : How to memoize the result of an effectful computation :
ZIO values are lazy and referentially transparent. So a IO[E,A] will be computed each time it needs to be evaluated.
It is a very intersting property in functional programming, but sometimes for long and cachable computations, it can be useful to memoize a result. It can be done using ZIO.memoize.
Here is an example :
val compute: Int => UIO[Int] = (x: Int) => ???
for {
      m <- ZIO.memoize(compute)
      a <- m(1)
      b <- m(1)
      c <- m(1)
      d <- m(2)
} yield(a, b, c, d)
With this code, m(1) result will only be computed 1 time.
  
  
  comments powered by Disqus