Class StateT<S,M extends MonadRec<?,M>,A>
java.lang.Object
com.jnape.palatable.lambda.monad.transformer.builtin.StateT<S,M,A>
- Type Parameters:
S
- the state typeM
- themonadic embedding
A
- the result type
- All Implemented Interfaces:
Applicative<A,
,StateT<S, M, ?>> Functor<A,
,StateT<S, M, ?>> Monad<A,
,StateT<S, M, ?>> MonadBase<M,
,A, StateT<S, ?, ?>> MonadReader<S,
,A, StateT<S, M, ?>> MonadRec<A,
,StateT<S, M, ?>> MonadWriter<S,
,A, StateT<S, M, ?>> MonadT<M,
A, StateT<S, M, ?>, StateT<S, ?, ?>>
public final class StateT<S,M extends MonadRec<?,M>,A>
extends Object
implements MonadT<M,A,StateT<S,M,?>,StateT<S,?,?>>, MonadReader<S,A,StateT<S,M,?>>, MonadWriter<S,A,StateT<S,M,?>>
The
State
monad transformer
.- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionUpdate the accumulated state.discardL
(Applicative<B, StateT<S, M, ?>> appB) Sequence both thisApplicative
andappB
, discarding thisApplicative's
result and returningappB
.discardR
(Applicative<B, StateT<S, M, ?>> appB) Sequence both thisApplicative
andappB
, discardingappB's
result and returning thisApplicative
.Run the stateful computation embedded in theMonad
, returning the result.Run the stateful computation embedded in theMonad
, returning the final state.Chain dependent computations that may continue or short-circuit based on previous results.Covariantly transmute this functor's parameter using the given mapping function.Given a function that produces a value inside amonadic effect
from a state, produce aStateT
that simply passes its state to the function and applies it.Given alazy
instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.Map the accumulation into a value and pair it with the current output.Modify thisMonadReader's
environment after reading it but before running the effect.Map both the result and the final state to a new result and final state inside theMonad
.pure
(B b) Lift the valueb
into this applicative functor.pureStateT
(Pure<M> pureM) Lift amonadic state
intoStateT
.Lift a state-sensitivemonadically embedded
computation intoStateT
.Lift amonadic value
intoStateT
.trampolineM
(Fn1<? super A, ? extends MonadRec<RecursiveResult<A, B>, StateT<S, M, ?>>> fn) Given some operation yielding aRecursiveResult
inside thisMonadRec
, internally trampoline the operation until it yields atermination
instruction.Map the final state to a new final state inside the samemonadic effect
using the provided function.Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.
-
Field Details
-
stateFn
-
-
Constructor Details
-
StateT
-
-
Method Details
-
runStateT
-
evalT
Run the stateful computation embedded in theMonad
, returning the result.- Type Parameters:
MA
- the inferredMonad
result- Parameters:
s
- the initial state- Returns:
- the result
-
execT
Run the stateful computation embedded in theMonad
, returning the final state.- Type Parameters:
MS
- the inferredMonad
result- Parameters:
s
- the initial state- Returns:
- the final state
-
mapStateT
public <N extends MonadRec<?,N>, StateT<S,B> N, mapStateTB> (Fn1<? super MonadRec<Tuple2<A, S>, M>, ? extends MonadRec<Tuple2<B, S>, N>> fn) Map both the result and the final state to a new result and final state inside theMonad
.- Type Parameters:
N
- the newmonadic embedding
for thisStateT
B
- the new state type- Parameters:
fn
- the mapping function- Returns:
- the mapped
StateT
-
withStateT
Map the final state to a new final state inside the samemonadic effect
using the provided function.- Parameters:
fn
- the state-mapping function- Returns:
- the mapped
StateT
-
listens
Map the accumulation into a value and pair it with the current output.- Specified by:
listens
in interfaceMonadWriter<S,
M extends MonadRec<?, M>, A> - Type Parameters:
B
- the mapped output- Parameters:
fn
- the mapping function- Returns:
- the updated
MonadWriter
-
censor
Update the accumulated state.- Specified by:
censor
in interfaceMonadWriter<S,
M extends MonadRec<?, M>, A> - Parameters:
fn
- the update function- Returns:
- the updated
MonadWriter
-
local
Modify thisMonadReader's
environment after reading it but before running the effect.- Specified by:
local
in interfaceMonadReader<S,
M extends MonadRec<?, M>, A> - Parameters:
fn
- the modification function- Returns:
- the
MonadReader
with a modified environment
-
flatMap
Chain dependent computations that may continue or short-circuit based on previous results.- Specified by:
flatMap
in interfaceMonad<S,
M extends MonadRec<?, M>> - Specified by:
flatMap
in interfaceMonadReader<S,
M extends MonadRec<?, M>, A> - Specified by:
flatMap
in interfaceMonadRec<S,
M extends MonadRec<?, M>> - Specified by:
flatMap
in interfaceMonadT<M extends MonadRec<?,
M>, A, StateT<S, M extends MonadRec<?, M>, ?>, StateT<S, ?, ?>> - Specified by:
flatMap
in interfaceMonadWriter<S,
M extends MonadRec<?, M>, A> - Type Parameters:
B
- the resulting monad parameter type- Parameters:
f
- the dependent computation over A- Returns:
- the new monad instance
-
pure
Lift the valueb
into this applicative functor.- Specified by:
pure
in interfaceApplicative<S,
M extends MonadRec<?, M>> - Specified by:
pure
in interfaceMonad<S,
M extends MonadRec<?, M>> - Specified by:
pure
in interfaceMonadReader<S,
M extends MonadRec<?, M>, A> - Specified by:
pure
in interfaceMonadRec<S,
M extends MonadRec<?, M>> - Specified by:
pure
in interfaceMonadT<M extends MonadRec<?,
M>, A, StateT<S, M extends MonadRec<?, M>, ?>, StateT<S, ?, ?>> - Specified by:
pure
in interfaceMonadWriter<S,
M extends MonadRec<?, M>, A> - Type Parameters:
B
- the type of the returned applicative's parameter- Parameters:
b
- the value- Returns:
- an instance of this applicative over b
-
fmap
Covariantly transmute this functor's parameter using the given mapping function. Generally this method is specialized to return an instance of the class implementing Functor.- Specified by:
fmap
in interfaceApplicative<S,
M extends MonadRec<?, M>> - Specified by:
fmap
in interfaceFunctor<S,
M extends MonadRec<?, M>> - Specified by:
fmap
in interfaceMonad<S,
M extends MonadRec<?, M>> - Specified by:
fmap
in interfaceMonadReader<S,
M extends MonadRec<?, M>, A> - Specified by:
fmap
in interfaceMonadRec<S,
M extends MonadRec<?, M>> - Specified by:
fmap
in interfaceMonadT<M extends MonadRec<?,
M>, A, StateT<S, M extends MonadRec<?, M>, ?>, StateT<S, ?, ?>> - Specified by:
fmap
in interfaceMonadWriter<S,
M extends MonadRec<?, M>, A> - Type Parameters:
B
- the new parameter type- Parameters:
fn
- the mapping function- Returns:
- a functor over B (the new parameter type)
-
zip
Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.- Specified by:
zip
in interfaceApplicative<S,
M extends MonadRec<?, M>> - Specified by:
zip
in interfaceMonad<S,
M extends MonadRec<?, M>> - Specified by:
zip
in interfaceMonadReader<S,
M extends MonadRec<?, M>, A> - Specified by:
zip
in interfaceMonadRec<S,
M extends MonadRec<?, M>> - Specified by:
zip
in interfaceMonadT<M extends MonadRec<?,
M>, A, StateT<S, M extends MonadRec<?, M>, ?>, StateT<S, ?, ?>> - Specified by:
zip
in interfaceMonadWriter<S,
M extends MonadRec<?, M>, A> - Type Parameters:
B
- the resulting applicative parameter type- Parameters:
appFn
- the other applicative instance- Returns:
- the mapped applicative
-
lazyZip
public <B> Lazy<StateT<S,M, lazyZipB>> (Lazy<? extends Applicative<Fn1<? super A, ? extends B>, StateT<S, M, ?>>> lazyAppFn) Given alazy
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.- Specified by:
lazyZip
in interfaceApplicative<S,
M extends MonadRec<?, M>> - Specified by:
lazyZip
in interfaceMonad<S,
M extends MonadRec<?, M>> - Specified by:
lazyZip
in interfaceMonadReader<S,
M extends MonadRec<?, M>, A> - Specified by:
lazyZip
in interfaceMonadRec<S,
M extends MonadRec<?, M>> - Specified by:
lazyZip
in interfaceMonadT<M extends MonadRec<?,
M>, A, StateT<S, M extends MonadRec<?, M>, ?>, StateT<S, ?, ?>> - Specified by:
lazyZip
in interfaceMonadWriter<S,
M extends MonadRec<?, M>, A> - Type Parameters:
B
- the resulting applicative parameter type- Parameters:
lazyAppFn
- the lazy other applicative instance- Returns:
- the mapped applicative
- See Also:
-
discardL
Sequence both thisApplicative
andappB
, discarding thisApplicative's
result and returningappB
. This is generally useful for sequentially performing side-effects.- Specified by:
discardL
in interfaceApplicative<S,
M extends MonadRec<?, M>> - Specified by:
discardL
in interfaceMonad<S,
M extends MonadRec<?, M>> - Specified by:
discardL
in interfaceMonadReader<S,
M extends MonadRec<?, M>, A> - Specified by:
discardL
in interfaceMonadRec<S,
M extends MonadRec<?, M>> - Specified by:
discardL
in interfaceMonadT<M extends MonadRec<?,
M>, A, StateT<S, M extends MonadRec<?, M>, ?>, StateT<S, ?, ?>> - Specified by:
discardL
in interfaceMonadWriter<S,
M extends MonadRec<?, M>, A> - Type Parameters:
B
- the type of the returned Applicative's parameter- Parameters:
appB
- the other Applicative- Returns:
- appB
-
discardR
Sequence both thisApplicative
andappB
, discardingappB's
result and returning thisApplicative
. This is generally useful for sequentially performing side-effects.- Specified by:
discardR
in interfaceApplicative<S,
M extends MonadRec<?, M>> - Specified by:
discardR
in interfaceMonad<S,
M extends MonadRec<?, M>> - Specified by:
discardR
in interfaceMonadReader<S,
M extends MonadRec<?, M>, A> - Specified by:
discardR
in interfaceMonadRec<S,
M extends MonadRec<?, M>> - Specified by:
discardR
in interfaceMonadT<M extends MonadRec<?,
M>, A, StateT<S, M extends MonadRec<?, M>, ?>, StateT<S, ?, ?>> - Specified by:
discardR
in interfaceMonadWriter<S,
M extends MonadRec<?, M>, A> - Type Parameters:
B
- the type of appB's parameter- Parameters:
appB
- the other Applicative- Returns:
- this Applicative
-
lift
- Specified by:
lift
in interfaceMonadBase<S,
M extends MonadRec<?, M>, A> - Specified by:
lift
in interfaceMonadT<M extends MonadRec<?,
M>, A, StateT<S, M extends MonadRec<?, M>, ?>, StateT<S, ?, ?>> - Type Parameters:
B
- theMonadRec
carrier typeN
- the argumentMonadRec
witness- Parameters:
mb
- the argumentMonadRec
- Returns:
- the new
MonadBase
-
trampolineM
public <B> StateT<S,M, trampolineMB> (Fn1<? super A, ? extends MonadRec<RecursiveResult<A, B>, StateT<S, M, ?>>> fn) Given some operation yielding aRecursiveResult
inside thisMonadRec
, internally trampoline the operation until it yields atermination
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.
-
get
-
gets
public static <S,M extends MonadRec<?, StateT<S,M>, A> M, getsA> (Fn1<? super S, ? extends MonadRec<A, M>> fn) Given a function that produces a value inside amonadic effect
from a state, produce aStateT
that simply passes its state to the function and applies it. -
modify
-
put
Lift amonadic state
intoStateT
. -
stateT
Lift amonadic value
intoStateT
. -
stateT
public static <S,M extends MonadRec<?, StateT<S,M>, A> M, stateTA> (Fn1<? super S, ? extends MonadRec<Tuple2<A, S>, M>> stateFn) Lift a state-sensitivemonadically embedded
computation intoStateT
. -
pureStateT
-
liftStateT
-