Class State<S,A>
java.lang.Object
com.jnape.palatable.lambda.functor.builtin.State<S,A>
- Type Parameters:
S
- the state typeA
- the result type
- All Implemented Interfaces:
Applicative<A,
,State<S, ?>> Functor<A,
,State<S, ?>> Monad<A,
,State<S, ?>> MonadReader<S,
,A, State<S, ?>> MonadRec<A,
,State<S, ?>> MonadWriter<S,
A, State<S, ?>>
public final class State<S,A>
extends Object
implements MonadRec<A,State<S,?>>, MonadReader<S,A,State<S,?>>, MonadWriter<S,A,State<S,?>>
The state
Monad
, useful for iteratively building up state and state-contextualized result.
For more information, read about the state monad.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionUpdate the accumulated state.discardL
(Applicative<B, State<S, ?>> appB) Sequence both thisApplicative
andappB
, discarding thisApplicative's
result and returningappB
.discardR
(Applicative<B, State<S, ?>> appB) Sequence both thisApplicative
andappB
, discardingappB's
result and returning thisApplicative
.Run the stateful computation, returning the result.Run the stateful computation, 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.static <A> State
<A, A> get()
Create aState
that simply returns back the initial state as both the result and the final statestatic <S,
A> State <S, A> Create aState
that maps its initial state into its result, but leaves the initial state unchanged.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.pure
(B b) Lift the valueb
into this applicative functor.put
(S s) Run the stateful computation, returning aTuple2
of the result and the final state.static <S,
A> State <S, A> state
(A a) Create aState
that returnsa
as its result and its initial state as its final state.static <S,
A> State <S, A> Create aState
fromstateFn
, a function that maps an initial state into a result and a final state.trampolineM
(Fn1<? super A, ? extends MonadRec<RecursiveResult<A, B>, State<S, ?>>> 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 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
-
State
-
-
Method Details
-
run
Run the stateful computation, returning aTuple2
of the result and the final state.- Parameters:
s
- the initial state- Returns:
- a
Tuple2
of the result and the final state.
-
eval
Run the stateful computation, returning the result.- Parameters:
s
- the initial state- Returns:
- the result
-
exec
Run the stateful computation, returning the final state.- Parameters:
s
- the initial state- Returns:
- the final state
-
mapState
Map both the result and the final state to a new result and final state.- Type Parameters:
B
- the new state type- Parameters:
fn
- the mapping function- Returns:
- the mapped
State
-
withState
Map the final state to a new final state using the provided function.- Parameters:
fn
- the state-mapping function- Returns:
- the mapped
State
-
local
Modify thisMonadReader's
environment after reading it but before running the effect.- Specified by:
local
in interfaceMonadReader<S,
A, State<S, ?>> - Parameters:
fn
- the modification function- Returns:
- the
MonadReader
with a modified environment
-
listens
Map the accumulation into a value and pair it with the current output.- Specified by:
listens
in interfaceMonadWriter<S,
A, State<S, ?>> - 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,
A, State<S, ?>> - Parameters:
fn
- the update function- Returns:
- the updated
MonadWriter
-
flatMap
Chain dependent computations that may continue or short-circuit based on previous results.- Specified by:
flatMap
in interfaceMonad<S,
A> - Specified by:
flatMap
in interfaceMonadReader<S,
A, State<S, ?>> - Specified by:
flatMap
in interfaceMonadRec<S,
A> - Specified by:
flatMap
in interfaceMonadWriter<S,
A, State<S, ?>> - 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,
A> - Specified by:
pure
in interfaceMonad<S,
A> - Specified by:
pure
in interfaceMonadReader<S,
A, State<S, ?>> - Specified by:
pure
in interfaceMonadRec<S,
A> - Specified by:
pure
in interfaceMonadWriter<S,
A, State<S, ?>> - 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,
A> - Specified by:
fmap
in interfaceFunctor<S,
A> - Specified by:
fmap
in interfaceMonad<S,
A> - Specified by:
fmap
in interfaceMonadReader<S,
A, State<S, ?>> - Specified by:
fmap
in interfaceMonadRec<S,
A> - Specified by:
fmap
in interfaceMonadWriter<S,
A, State<S, ?>> - 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,
A> - Specified by:
zip
in interfaceMonad<S,
A> - Specified by:
zip
in interfaceMonadReader<S,
A, State<S, ?>> - Specified by:
zip
in interfaceMonadRec<S,
A> - Specified by:
zip
in interfaceMonadWriter<S,
A, State<S, ?>> - Type Parameters:
B
- the resulting applicative parameter type- Parameters:
appFn
- the other applicative instance- Returns:
- the mapped applicative
-
lazyZip
public <B> Lazy<State<S,B>> lazyZip(Lazy<? extends Applicative<Fn1<? super A, ? extends B>, State<S, ?>>> 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,
A> - Specified by:
lazyZip
in interfaceMonad<S,
A> - Specified by:
lazyZip
in interfaceMonadReader<S,
A, State<S, ?>> - Specified by:
lazyZip
in interfaceMonadRec<S,
A> - Specified by:
lazyZip
in interfaceMonadWriter<S,
A, State<S, ?>> - Type Parameters:
B
- the resulting applicative parameter type- Parameters:
lazyAppFn
- the lazy other applicative instance- Returns:
- the mapped applicative
- See Also:
-
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,
A> - Specified by:
discardR
in interfaceMonad<S,
A> - Specified by:
discardR
in interfaceMonadReader<S,
A, State<S, ?>> - Specified by:
discardR
in interfaceMonadRec<S,
A> - Specified by:
discardR
in interfaceMonadWriter<S,
A, State<S, ?>> - Type Parameters:
B
- the type of appB's parameter- Parameters:
appB
- the other Applicative- Returns:
- this Applicative
-
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,
A> - Specified by:
discardL
in interfaceMonad<S,
A> - Specified by:
discardL
in interfaceMonadReader<S,
A, State<S, ?>> - Specified by:
discardL
in interfaceMonadRec<S,
A> - Specified by:
discardL
in interfaceMonadWriter<S,
A, State<S, ?>> - Type Parameters:
B
- the type of the returned Applicative's parameter- Parameters:
appB
- the other Applicative- Returns:
- appB
-
trampolineM
public <B> State<S,B> trampolineM(Fn1<? super A, ? extends MonadRec<RecursiveResult<A, B>, State<S, ?>>> 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.
- Specified by:
trampolineM
in interfaceMonadRec<S,
A> - Type Parameters:
B
- the ultimate resulting carrier type- Parameters:
fn
- the function to internally trampoline- Returns:
- the trampolined
MonadRec
- See Also:
-
get
Create aState
that simply returns back the initial state as both the result and the final state- Type Parameters:
A
- the state and result type- Returns:
- the new
State
instance
-
put
- Type Parameters:
S
- the state type- Parameters:
s
- the final state- Returns:
- the new
State
instance
-
gets
Create aState
that maps its initial state into its result, but leaves the initial state unchanged.- Type Parameters:
S
- the state typeA
- the result type- Parameters:
fn
- the mapping function- Returns:
- the new
State
instance
-
modify
- Type Parameters:
S
- the state type- Parameters:
fn
- the mapping function- Returns:
- the new
State
instance
-
state
Create aState
that returnsa
as its result and its initial state as its final state.- Type Parameters:
S
- the state typeA
- the result type- Parameters:
a
- the result- Returns:
- the new
State
instance
-
state
Create aState
fromstateFn
, a function that maps an initial state into a result and a final state.- Type Parameters:
S
- the state typeA
- the result type- Parameters:
stateFn
- the state function- Returns:
- the new
State
instance
-
pureState
- Type Parameters:
S
- the state type- Returns:
- the
Pure
instance
-