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 thisApplicativeandappB, discarding thisApplicative'sresult and returningappB.discardR(Applicative<B, State<S, ?>> appB) Sequence both thisApplicativeandappB, discardingappB'sresult 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 aStatethat simply returns back the initial state as both the result and the final statestatic <S,A> State <S, A> Create aStatethat maps its initial state into its result, but leaves the initial state unchanged.Given alazyinstance 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'senvironment 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 valuebinto this applicative functor.put(S s) Run the stateful computation, returning aTuple2of the result and the final state.static <S,A> State <S, A> state(A a) Create aStatethat returnsaas its result and its initial state as its final state.static <S,A> State <S, A> Create aStatefromstateFn, 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 aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.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 aTuple2of the result and the final state.- Parameters:
s- the initial state- Returns:
- a
Tuple2of 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'senvironment after reading it but before running the effect.- Specified by:
localin interfaceMonadReader<S,A, State<S, ?>> - Parameters:
fn- the modification function- Returns:
- the
MonadReaderwith a modified environment
-
listens
Map the accumulation into a value and pair it with the current output.- Specified by:
listensin 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:
censorin 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:
flatMapin interfaceMonad<S,A> - Specified by:
flatMapin interfaceMonadReader<S,A, State<S, ?>> - Specified by:
flatMapin interfaceMonadRec<S,A> - Specified by:
flatMapin 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 valuebinto this applicative functor.- Specified by:
purein interfaceApplicative<S,A> - Specified by:
purein interfaceMonad<S,A> - Specified by:
purein interfaceMonadReader<S,A, State<S, ?>> - Specified by:
purein interfaceMonadRec<S,A> - Specified by:
purein 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:
fmapin interfaceApplicative<S,A> - Specified by:
fmapin interfaceFunctor<S,A> - Specified by:
fmapin interfaceMonad<S,A> - Specified by:
fmapin interfaceMonadReader<S,A, State<S, ?>> - Specified by:
fmapin interfaceMonadRec<S,A> - Specified by:
fmapin 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:
zipin interfaceApplicative<S,A> - Specified by:
zipin interfaceMonad<S,A> - Specified by:
zipin interfaceMonadReader<S,A, State<S, ?>> - Specified by:
zipin interfaceMonadRec<S,A> - Specified by:
zipin 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 alazyinstance 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:
lazyZipin interfaceApplicative<S,A> - Specified by:
lazyZipin interfaceMonad<S,A> - Specified by:
lazyZipin interfaceMonadReader<S,A, State<S, ?>> - Specified by:
lazyZipin interfaceMonadRec<S,A> - Specified by:
lazyZipin 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 thisApplicativeandappB, discardingappB'sresult and returning thisApplicative. This is generally useful for sequentially performing side-effects.- Specified by:
discardRin interfaceApplicative<S,A> - Specified by:
discardRin interfaceMonad<S,A> - Specified by:
discardRin interfaceMonadReader<S,A, State<S, ?>> - Specified by:
discardRin interfaceMonadRec<S,A> - Specified by:
discardRin 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 thisApplicativeandappB, discarding thisApplicative'sresult and returningappB. This is generally useful for sequentially performing side-effects.- Specified by:
discardLin interfaceApplicative<S,A> - Specified by:
discardLin interfaceMonad<S,A> - Specified by:
discardLin interfaceMonadReader<S,A, State<S, ?>> - Specified by:
discardLin interfaceMonadRec<S,A> - Specified by:
discardLin 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 aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.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:
trampolineMin 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 aStatethat 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
Stateinstance
-
put
- Type Parameters:
S- the state type- Parameters:
s- the final state- Returns:
- the new
Stateinstance
-
gets
Create aStatethat 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
Stateinstance
-
modify
- Type Parameters:
S- the state type- Parameters:
fn- the mapping function- Returns:
- the new
Stateinstance
-
state
Create aStatethat returnsaas 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
Stateinstance
-
state
Create aStatefromstateFn, 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
Stateinstance
-
pureState
- Type Parameters:
S- the state type- Returns:
- the
Pureinstance
-