S
- the state typeA
- the result typepublic final class State<S,A> extends Object implements MonadRec<A,State<S,?>>, MonadReader<S,A,State<S,?>>, MonadWriter<S,A,State<S,?>>
Monad
, useful for iteratively building up state and state-contextualized result.
For more information, read about the state monad.
Modifier and Type | Method and Description |
---|---|
State<S,A> |
censor(Fn1<? super S,? extends S> fn)
Update the accumulated state.
|
<B> State<S,B> |
discardL(Applicative<B,State<S,?>> appB)
Sequence both this
Applicative and appB , discarding this Applicative's
result and returning appB . |
<B> State<S,A> |
discardR(Applicative<B,State<S,?>> appB)
Sequence both this
Applicative and appB , discarding appB's result and
returning this Applicative . |
A |
eval(S s)
Run the stateful computation, returning the result.
|
S |
exec(S s)
Run the stateful computation, returning the final state.
|
<B> State<S,B> |
flatMap(Fn1<? super A,? extends Monad<B,State<S,?>>> f)
Chain dependent computations that may continue or short-circuit based on previous results.
|
<B> State<S,B> |
fmap(Fn1<? super A,? extends B> fn)
Covariantly transmute this functor's parameter using the given mapping function.
|
static <A> State<A,A> |
get()
Create a
State that simply returns back the initial state as both the result and the final state |
static <S,A> State<S,A> |
gets(Fn1<? super S,? extends A> fn)
Create a
State that maps its initial state into its result, but leaves the initial state unchanged. |
<B> Lazy<State<S,B>> |
lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,State<S,?>>> 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> State<S,Tuple2<A,B>> |
listens(Fn1<? super S,? extends B> fn)
Map the accumulation into a value and pair it with the current output.
|
State<S,A> |
local(Fn1<? super S,? extends S> fn)
Modify this
MonadReader's environment after reading it but before running the effect. |
<B> State<S,B> |
mapState(Fn1<? super Tuple2<A,S>,? extends Tuple2<B,S>> fn)
Map both the result and the final state to a new result and final state.
|
static <S> State<S,Unit> |
modify(Fn1<? super S,? extends S> fn)
|
<B> State<S,B> |
pure(B b)
Lift the value
b into this applicative functor. |
static <S> Pure<State<S,?>> |
pureState()
|
static <S> State<S,Unit> |
put(S s)
|
Tuple2<A,S> |
run(S s)
Run the stateful computation, returning a
Tuple2 of the result and the final state. |
static <S,A> State<S,A> |
state(A a)
Create a
State that returns a as its result and its initial state as its final state. |
static <S,A> State<S,A> |
state(Fn1<? super S,? extends Tuple2<A,S>> stateFn)
Create a
State from stateFn , a function that maps an initial state into a result and a final
state. |
<B> State<S,B> |
trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,State<S,?>>> fn)
Given some operation yielding a
RecursiveResult inside this MonadRec , internally trampoline the
operation until it yields a termination instruction. |
State<S,A> |
withState(Fn1<? super S,? extends S> fn)
Map the final state to a new final state using the provided function.
|
<B> State<S,B> |
zip(Applicative<Fn1<? super A,? extends B>,State<S,?>> appFn)
Given another instance of this applicative over a mapping function, "zip" the two instances together using
whatever application semantics the current applicative supports.
|
public Tuple2<A,S> run(S s)
Tuple2
of the result and the final state.s
- the initial stateTuple2
of the result and the final state.public A eval(S s)
s
- the initial statepublic S exec(S s)
s
- the initial statepublic <B> State<S,B> mapState(Fn1<? super Tuple2<A,S>,? extends Tuple2<B,S>> fn)
B
- the new state typefn
- the mapping functionState
public State<S,A> withState(Fn1<? super S,? extends S> fn)
fn
- the state-mapping functionState
public State<S,A> local(Fn1<? super S,? extends S> fn)
MonadReader's
environment after reading it but before running the effect.local
in interface MonadReader<S,A,State<S,?>>
fn
- the modification functionMonadReader
with a modified environmentpublic <B> State<S,Tuple2<A,B>> listens(Fn1<? super S,? extends B> fn)
listens
in interface MonadWriter<S,A,State<S,?>>
B
- the mapped outputfn
- the mapping functionMonadWriter
public State<S,A> censor(Fn1<? super S,? extends S> fn)
censor
in interface MonadWriter<S,A,State<S,?>>
fn
- the update functionMonadWriter
public <B> State<S,B> flatMap(Fn1<? super A,? extends Monad<B,State<S,?>>> f)
flatMap
in interface Monad<A,State<S,?>>
flatMap
in interface MonadReader<S,A,State<S,?>>
flatMap
in interface MonadRec<A,State<S,?>>
flatMap
in interface MonadWriter<S,A,State<S,?>>
B
- the resulting monad parameter typef
- the dependent computation over Apublic <B> State<S,B> pure(B b)
b
into this applicative functor.pure
in interface Applicative<A,State<S,?>>
pure
in interface Monad<A,State<S,?>>
pure
in interface MonadReader<S,A,State<S,?>>
pure
in interface MonadRec<A,State<S,?>>
pure
in interface MonadWriter<S,A,State<S,?>>
B
- the type of the returned applicative's parameterb
- the valuepublic <B> State<S,B> fmap(Fn1<? super A,? extends B> fn)
fmap
in interface Applicative<A,State<S,?>>
fmap
in interface Functor<A,State<S,?>>
fmap
in interface Monad<A,State<S,?>>
fmap
in interface MonadReader<S,A,State<S,?>>
fmap
in interface MonadRec<A,State<S,?>>
fmap
in interface MonadWriter<S,A,State<S,?>>
B
- the new parameter typefn
- the mapping functionpublic <B> State<S,B> zip(Applicative<Fn1<? super A,? extends B>,State<S,?>> appFn)
zip
in interface Applicative<A,State<S,?>>
zip
in interface Monad<A,State<S,?>>
zip
in interface MonadReader<S,A,State<S,?>>
zip
in interface MonadRec<A,State<S,?>>
zip
in interface MonadWriter<S,A,State<S,?>>
B
- the resulting applicative parameter typeappFn
- the other applicative instancepublic <B> Lazy<State<S,B>> lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,State<S,?>>> 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,State<S,?>>
lazyZip
in interface Monad<A,State<S,?>>
lazyZip
in interface MonadReader<S,A,State<S,?>>
lazyZip
in interface MonadRec<A,State<S,?>>
lazyZip
in interface MonadWriter<S,A,State<S,?>>
B
- the resulting applicative parameter typelazyAppFn
- the lazy other applicative instanceMaybe
,
Either
public <B> State<S,A> discardR(Applicative<B,State<S,?>> appB)
Applicative
and appB
, discarding appB's
result and
returning this Applicative
. This is generally useful for sequentially performing side-effects.discardR
in interface Applicative<A,State<S,?>>
discardR
in interface Monad<A,State<S,?>>
discardR
in interface MonadReader<S,A,State<S,?>>
discardR
in interface MonadRec<A,State<S,?>>
discardR
in interface MonadWriter<S,A,State<S,?>>
B
- the type of appB's parameterappB
- the other Applicativepublic <B> State<S,B> discardL(Applicative<B,State<S,?>> appB)
Applicative
and appB
, discarding this Applicative's
result and returning appB
. This is generally useful for sequentially performing side-effects.discardL
in interface Applicative<A,State<S,?>>
discardL
in interface Monad<A,State<S,?>>
discardL
in interface MonadReader<S,A,State<S,?>>
discardL
in interface MonadRec<A,State<S,?>>
discardL
in interface MonadWriter<S,A,State<S,?>>
B
- the type of the returned Applicative's parameterappB
- the other Applicativepublic <B> State<S,B> trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,State<S,?>>> 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.
trampolineM
in interface MonadRec<A,State<S,?>>
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
public static <A> State<A,A> get()
State
that simply returns back the initial state as both the result and the final stateA
- the state and result typeState
instancepublic static <S> State<S,Unit> put(S s)
S
- the state types
- the final stateState
instancepublic static <S,A> State<S,A> gets(Fn1<? super S,? extends A> fn)
State
that maps its initial state into its result, but leaves the initial state unchanged.S
- the state typeA
- the result typefn
- the mapping functionState
instancepublic static <S> State<S,Unit> modify(Fn1<? super S,? extends S> fn)
S
- the state typefn
- the mapping functionState
instancepublic static <S,A> State<S,A> state(A a)
State
that returns a
as its result and its initial state as its final state.S
- the state typeA
- the result typea
- the resultState
instancepublic static <S,A> State<S,A> state(Fn1<? super S,? extends Tuple2<A,S>> stateFn)
State
from stateFn
, a function that maps an initial state into a result and a final
state.S
- the state typeA
- the result typestateFn
- the state functionState
instance