jam.monad

package jam.monad

Members list

Concise view

Type members

Classlikes

sealed abstract class Reval[F[_], +A]

Reval is a data structure which encodes the idea of allocating an object which has an associated finalizer. Can be thought of as a mix of cats.effect.Resource and cats.Eval. There are two allocation strategies:

Reval is a data structure which encodes the idea of allocating an object which has an associated finalizer. Can be thought of as a mix of cats.effect.Resource and cats.Eval. There are two allocation strategies:

  • later: allocates the object once when it is needed
  • always: allocates the object every time it is needed

For example:

scala> var c = 0
scala> val a = Reval.thunkLater[IO, Int]{c += 1; c}
scala> a.replicateA(3).map(_.sum).usePure.unsafeRunSync() // List(1, 1, 1).sum
val res0: Int = 3
scala> var c = 0
scala> val a = Reval.thunkAlways[IO, Int]{c += 1; c}
scala> a.replicateA(3).map(_.sum).usePure.unsafeRunSync() // List(1, 2, 3).sum
val res0: Int = 6

Common pitfalls:

  • later with multiple use calls will allocate later objects for each use
scala> var c = 0
scala> val a = Reval.thunkLater[IO, Int] { c += 1; c }
scala> (a.usePure, a.usePure).mapN(_ + _).unsafeRunSync() // 1 + 2
val res1: Int = 3

Attributes

Companion:
object
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class Eval[F, A]
class FlatMap[F, A, B]
class Memoize[F, A]
object Reval extends RevalInstances

Attributes

Companion:
class
Graph
Supertypes
trait Sum
trait Mirror
class Object
trait Matchable
class Any
Self type
Reval.type