Class Writer<W,A>
java.lang.Object
com.jnape.palatable.lambda.functor.builtin.Writer<W,A>
- Type Parameters:
W
- the accumulation typeA
- the value type
- All Implemented Interfaces:
Applicative<A,
,Writer<W, ?>> Functor<A,
,Writer<W, ?>> Monad<A,
,Writer<W, ?>> MonadRec<A,
,Writer<W, ?>> MonadWriter<W,
A, Writer<W, ?>>
public final class Writer<W,A>
extends Object
implements MonadWriter<W,A,Writer<W,?>>, MonadRec<A,Writer<W,?>>
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionUpdate the accumulated state.discardL
(Applicative<B, Writer<W, ?>> appB) Sequence both thisApplicative
andappB
, discarding thisApplicative's
result and returningappB
.discardR
(Applicative<B, Writer<W, ?>> appB) Sequence both thisApplicative
andappB
, discardingappB's
result and returning thisApplicative
.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 alazy
instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.static <W,
A> Writer <W, A> listen
(A a) Construct aWriter
from a value.Map the accumulation into a value and pair it with the current output.pure
(B b) Lift the valueb
into this applicative functor.tell
(W w) Construct aWriter
from an accumulation.trampolineM
(Fn1<? super A, ? extends MonadRec<RecursiveResult<A, B>, Writer<W, ?>>> fn) Given some operation yielding aRecursiveResult
inside thisMonadRec
, internally trampoline the operation until it yields atermination
instruction.static <W,
A> Writer <W, A> Construct aWriter
from an accumulation and a value.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
-
writerFn
-
-
Constructor Details
-
Writer
-
-
Method Details
-
runWriter
Given aMonoid
for the accumulation, run the computation represented by thisWriter
, accumulate the written output in terms of theMonoid
, and produce the accumulation and the value.- Parameters:
monoid
- the accumulationMonoid
- Returns:
- the accumulation with the value
-
listens
Map the accumulation into a value and pair it with the current output.- Specified by:
listens
in interfaceMonadWriter<W,
A, Writer<W, ?>> - 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<W,
A, Writer<W, ?>> - Parameters:
fn
- the update function- Returns:
- the updated
MonadWriter
-
trampolineM
public <B> Writer<W,B> trampolineM(Fn1<? super A, ? extends MonadRec<RecursiveResult<A, B>, Writer<W, ?>>> 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<W,
A> - Type Parameters:
B
- the ultimate resulting carrier type- Parameters:
fn
- the function to internally trampoline- Returns:
- the trampolined
MonadRec
- See Also:
-
flatMap
Chain dependent computations that may continue or short-circuit based on previous results. -
pure
Lift the valueb
into this applicative functor.- Specified by:
pure
in interfaceApplicative<W,
A> - Specified by:
pure
in interfaceMonad<W,
A> - Specified by:
pure
in interfaceMonadRec<W,
A> - Specified by:
pure
in interfaceMonadWriter<W,
A, Writer<W, ?>> - 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<W,
A> - Specified by:
fmap
in interfaceFunctor<W,
A> - Specified by:
fmap
in interfaceMonad<W,
A> - Specified by:
fmap
in interfaceMonadRec<W,
A> - Specified by:
fmap
in interfaceMonadWriter<W,
A, Writer<W, ?>> - 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<W,
A> - Specified by:
zip
in interfaceMonad<W,
A> - Specified by:
zip
in interfaceMonadRec<W,
A> - Specified by:
zip
in interfaceMonadWriter<W,
A, Writer<W, ?>> - Type Parameters:
B
- the resulting applicative parameter type- Parameters:
appFn
- the other applicative instance- Returns:
- the mapped applicative
-
lazyZip
public <B> Lazy<Writer<W,B>> lazyZip(Lazy<? extends Applicative<Fn1<? super A, ? extends B>, Writer<W, ?>>> 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<W,
A> - Specified by:
lazyZip
in interfaceMonad<W,
A> - Specified by:
lazyZip
in interfaceMonadRec<W,
A> - Specified by:
lazyZip
in interfaceMonadWriter<W,
A, Writer<W, ?>> - 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<W,
A> - Specified by:
discardL
in interfaceMonad<W,
A> - Specified by:
discardL
in interfaceMonadRec<W,
A> - Specified by:
discardL
in interfaceMonadWriter<W,
A, Writer<W, ?>> - 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<W,
A> - Specified by:
discardR
in interfaceMonad<W,
A> - Specified by:
discardR
in interfaceMonadRec<W,
A> - Specified by:
discardR
in interfaceMonadWriter<W,
A, Writer<W, ?>> - Type Parameters:
B
- the type of appB's parameter- Parameters:
appB
- the other Applicative- Returns:
- this Applicative
-
tell
Construct aWriter
from an accumulation.- Type Parameters:
W
- the accumulation type- Parameters:
w
- the accumulation- Returns:
- the
Writer
-
listen
Construct aWriter
from a value.- Type Parameters:
W
- the accumulation typeA
- the value type- Parameters:
a
- the output value- Returns:
- the
Writer
-
writer
Construct aWriter
from an accumulation and a value.- Type Parameters:
W
- the accumulation typeA
- the value type- Parameters:
aw
- the output value and accumulation- Returns:
- the
WriterT
-
pureWriter
- Type Parameters:
W
- the accumulation type- Returns:
- the
Pure
instance
-