common sealed class Eval<out A>
Eval is a monad which controls evaluation of a value or a computation that produces a value.
Three basic evaluation strategies:
The Later and Always are both lazy strategies while Now is eager. Later and Always are distinguished from each other only by memoization: once evaluated Later will save the value to be returned immediately if it is needed again. Always will run its computation every time.
methods, which use an internal trampoline to avoid stack overflows. Computation done within .map and .flatMap is always done lazily, even when applied to a Now instance.
It is not generally good style to pattern-match on Eval instances. Rather, use .map and .flatMap to chain computation, and use .value to get the result when needed. It is also not good style to create Eval instances whose computation involves calling .value on another Eval instance – this can defeat the trampolining and lead to stack overflows.
Example of stack safety:
import arrow.core.Eval
//sampleStart
fun even(n: Int): Eval<Boolean> =
Eval.always { n == 0 }.flatMap {
if(it == true) Eval.now(true)
else odd(n - 1)
}
fun odd(n: Int): Eval<Boolean> =
Eval.always { n == 0 }.flatMap {
if(it == true) Eval.now(false)
else even(n - 1)
}
// if not wrapped in eval this type of computation would blow the stack and result in a StackOverflowError
fun main() {
println(odd(100000).value())
}
//sampleEnd
Name | Summary |
---|---|
Always | common data class Always<out A>(f: () -> A) : Eval<A> Construct a lazy Eval instance. |
Companion | common object Companion |
Defer | common data class Defer<out A>(val thunk: () -> Eval<A>) : Eval<A> Defer is a type of Eval that is used to defer computations which produce Eval. |
FlatMap | common abstract class FlatMap<out A> : Eval<A> FlatMap is a type of Eval that is used to chain computations involving .map and .flatMap. Along with Eval#flatMap. It implements the trampoline that guarantees stack-safety. |
Later | common data class Later<out A>(f: () -> A) : Eval<A> Construct a lazy Eval instance. |
Now | common data class Now<out A>(val value: A) : Eval<A> Construct an eager Eval instance. In some sense it is equivalent to using a val. |
Name | Summary |
---|---|
coflatMap | common inline fun <B> coflatMap(crossinline f: (Eval<A>) -> B): Eval<B> |
flatMap | common fun <B> flatMap(f: (A) -> Eval<B>): Eval<B> |
map | common inline fun <B> map(crossinline f: (A) -> B): Eval<B> |
memoize | common abstract fun memoize(): Eval<A> |
toString | common open override fun toString(): String |
value | common abstract fun value(): A |
Name |
---|
Now |
Later |
Always |
Defer |
FlatMap |
Name | Summary |
---|---|
replicate | common fun <A> Eval<A>.replicate(n: Int): Eval<List<A» fun <A> Eval<A>.replicate(n: Int, MA: Monoid<A>): Eval<A> |
zip | common fun <A, B, Z> Eval<A>.zip(b: Eval<B>, map: (A, B) -> Z): Eval<Z> fun <A, B> Eval<A>.zip(b: Eval<B>): Eval<Pair<A, B» fun <A, B, C, D> Eval<A>.zip(b: Eval<B>, c: Eval<C>, map: (A, B, C) -> D): Eval<D> fun <A, B, C, D, E> Eval<A>.zip(b: Eval<B>, c: Eval<C>, d: Eval<D>, map: (A, B, C, D) -> E): Eval<E> fun <A, B, C, D, E, F> Eval<A>.zip(b: Eval<B>, c: Eval<C>, d: Eval<D>, e: Eval<E>, map: (A, B, C, D, E) -> F): Eval<F> fun <A, B, C, D, E, F, G> Eval<A>.zip(b: Eval<B>, c: Eval<C>, d: Eval<D>, e: Eval<E>, f: Eval<F>, map: (A, B, C, D, E, F) -> G): Eval<G> fun <A, B, C, D, E, F, G, H> Eval<A>.zip(b: Eval<B>, c: Eval<C>, d: Eval<D>, e: Eval<E>, f: Eval<F>, g: Eval<G>, map: (A, B, C, D, E, F, G) -> H): Eval<H> fun <A, B, C, D, E, F, G, H, I> Eval<A>.zip(b: Eval<B>, c: Eval<C>, d: Eval<D>, e: Eval<E>, f: Eval<F>, g: Eval<G>, h: Eval<H>, map: (A, B, C, D, E, F, G, H) -> I): Eval<I> fun <A, B, C, D, E, F, G, H, I, J> Eval<A>.zip(b: Eval<B>, c: Eval<C>, d: Eval<D>, e: Eval<E>, f: Eval<F>, g: Eval<G>, h: Eval<H>, i: Eval<I>, map: (A, B, C, D, E, F, G, H, I) -> J): Eval<J> fun <A, B, C, D, E, F, G, H, I, J, K> Eval<A>.zip(b: Eval<B>, c: Eval<C>, d: Eval<D>, e: Eval<E>, f: Eval<F>, g: Eval<G>, h: Eval<H>, i: Eval<I>, j: Eval<J>, map: (A, B, C, D, E, F, G, H, I, J) -> K): Eval<K> |
Do you like Arrow?
✖