A
- the carrier typeM
- the witness
public interface MonadRec<A,M extends MonadRec<?,M>> extends Monad<A,M>
monads
that offer a stack-safe interface for performing arbitrarily many
flatmap-like
operations via trampolineM(Fn1)
.
Inspired by Phil Freeman's paper _Stack Safety for Free_
Modifier and Type | Method and Description |
---|---|
default <B> MonadRec<B,M> |
discardL(Applicative<B,M> appB)
Sequence both this
Applicative and appB , discarding this Applicative's
result and returning appB . |
default <B> MonadRec<A,M> |
discardR(Applicative<B,M> appB)
Sequence both this
Applicative and appB , discarding appB's result and
returning this Applicative . |
<B> MonadRec<B,M> |
flatMap(Fn1<? super A,? extends Monad<B,M>> f)
Chain dependent computations that may continue or short-circuit based on previous results.
|
default <B> MonadRec<B,M> |
fmap(Fn1<? super A,? extends B> fn)
Covariantly transmute this functor's parameter using the given mapping function.
|
default <B> Lazy<? extends MonadRec<B,M>> |
lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,M>> lazyAppFn)
Given a
lazy instance of this applicative over a mapping function, "zip" the two instances together
using whatever application semantics the current applicative supports. |
<B> MonadRec<B,M> |
pure(B b)
Lift the value
b into this applicative functor. |
<B> MonadRec<B,M> |
trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,M>> fn)
Given some operation yielding a
RecursiveResult inside this MonadRec , internally trampoline the
operation until it yields a termination instruction. |
default <B> MonadRec<B,M> |
zip(Applicative<Fn1<? super A,? extends B>,M> appFn)
Given another instance of this applicative over a mapping function, "zip" the two instances together using
whatever application semantics the current applicative supports.
|
<B> MonadRec<B,M> trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,M>> fn)
RecursiveResult
inside this MonadRec
, internally trampoline the
operation until it yields a termination
instruction.
Stack-safety depends on implementations guaranteeing that the growth of the call stack is a constant factor independent of the number of invocations of the operation. For various examples of how this can be achieved in stereotypical circumstances, see the referenced types.
B
- the ultimate resulting carrier typefn
- the function to internally trampolineMonadRec
for a basic implementation
,
for a {@link CoProduct2 coproduct} implementation
,
for an implementation leveraging an already stack-safe {@link Monad#flatMap(Fn1)}
,
for a {@link MonadT monad transformer} implementation
<B> MonadRec<B,M> flatMap(Fn1<? super A,? extends Monad<B,M>> f)
default <B> MonadRec<B,M> fmap(Fn1<? super A,? extends B> fn)
fmap
in interface Applicative<A,M extends MonadRec<?,M>>
fmap
in interface Functor<A,M extends MonadRec<?,M>>
fmap
in interface Monad<A,M extends MonadRec<?,M>>
B
- the new parameter typefn
- the mapping functiondefault <B> MonadRec<B,M> zip(Applicative<Fn1<? super A,? extends B>,M> appFn)
default <B> Lazy<? extends MonadRec<B,M>> lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,M>> lazyAppFn)
lazy
instance of this applicative over a mapping function, "zip" the two instances together
using whatever application semantics the current applicative supports. This is useful for applicatives that
support lazy evaluation and early termination.lazyZip
in interface Applicative<A,M extends MonadRec<?,M>>
lazyZip
in interface Monad<A,M extends MonadRec<?,M>>
B
- the resulting applicative parameter typelazyAppFn
- the lazy other applicative instanceMaybe
,
Either
default <B> MonadRec<B,M> discardL(Applicative<B,M> appB)
Applicative
and appB
, discarding this Applicative's
result and returning appB
. This is generally useful for sequentially performing side-effects.default <B> MonadRec<A,M> discardR(Applicative<B,M> appB)
Applicative
and appB
, discarding appB's
result and
returning this Applicative
. This is generally useful for sequentially performing side-effects.