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 java.lang.Object implements MonadWriter<W,A,Writer<W,?>>, MonadRec<A,Writer<W,?>>
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Writer<W,A>censor(Fn1<? super W,? extends W> fn)Update the accumulated state.<B> Writer<W,B>discardL(Applicative<B,Writer<W,?>> appB)Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB.<B> Writer<W,A>discardR(Applicative<B,Writer<W,?>> appB)Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative.<B> Writer<W,B>flatMap(Fn1<? super A,? extends Monad<B,Writer<W,?>>> f)Chain dependent computations that may continue or short-circuit based on previous results.<B> Writer<W,B>fmap(Fn1<? super A,? extends B> fn)Covariantly transmute this functor's parameter using the given mapping function.<B> Lazy<Writer<W,B>>lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,Writer<W,?>>> lazyAppFn)Given alazyinstance 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 aWriterfrom a value.<B> Writer<W,Tuple2<A,B>>listens(Fn1<? super W,? extends B> fn)Map the accumulation into a value and pair it with the current output.<B> Writer<W,B>pure(B b)Lift the valuebinto this applicative functor.static <W> Pure<Writer<W,?>>pureWriter()Tuple2<A,W>runWriter(Monoid<W> monoid)static <W> Writer<W,Unit>tell(W w)Construct aWriterfrom an accumulation.<B> Writer<W,B>trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,Writer<W,?>>> fn)Given some operation yielding aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.static <W,A>
Writer<W,A>writer(Tuple2<A,W> aw)Construct aWriterfrom an accumulation and a value.<B> Writer<W,B>zip(Applicative<Fn1<? super A,? extends B>,Writer<W,?>> appFn)Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.
-
-
-
Method Detail
-
runWriter
public Tuple2<A,W> runWriter(Monoid<W> monoid)
Given aMonoidfor 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
public <B> Writer<W,Tuple2<A,B>> listens(Fn1<? super W,? extends B> fn)
Map the accumulation into a value and pair it with the current output.- Specified by:
listensin interfaceMonadWriter<W,A,Writer<W,?>>- Type Parameters:
B- the mapped output- Parameters:
fn- the mapping function- Returns:
- the updated
MonadWriter
-
censor
public Writer<W,A> censor(Fn1<? super W,? extends W> fn)
Update the accumulated state.- Specified by:
censorin 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 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<W,A>- Type Parameters:
B- the ultimate resulting carrier type- Parameters:
fn- the function to internally trampoline- Returns:
- the trampolined
MonadRec - See Also:
for a basic implementation,for a implementation,for an implementation leveraging an already stack-safe,for a implementation
-
flatMap
public <B> Writer<W,B> flatMap(Fn1<? super A,? extends Monad<B,Writer<W,?>>> f)
Chain dependent computations that may continue or short-circuit based on previous results.- Specified by:
flatMapin interfaceMonad<W,A>- Specified by:
flatMapin interfaceMonadRec<W,A>- Specified by:
flatMapin interfaceMonadWriter<W,A,Writer<W,?>>- Type Parameters:
B- the resulting monad parameter type- Parameters:
f- the dependent computation over A- Returns:
- the new monad instance
-
pure
public <B> Writer<W,B> pure(B b)
Lift the valuebinto this applicative functor.- Specified by:
purein interfaceApplicative<W,A>- Specified by:
purein interfaceMonad<W,A>- Specified by:
purein interfaceMonadRec<W,A>- Specified by:
purein 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
public <B> Writer<W,B> fmap(Fn1<? super A,? extends B> fn)
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<W,A>- Specified by:
fmapin interfaceFunctor<W,A>- Specified by:
fmapin interfaceMonad<W,A>- Specified by:
fmapin interfaceMonadRec<W,A>- Specified by:
fmapin 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
public <B> Writer<W,B> zip(Applicative<Fn1<? super A,? extends B>,Writer<W,?>> appFn)
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<W,A>- Specified by:
zipin interfaceMonad<W,A>- Specified by:
zipin interfaceMonadRec<W,A>- Specified by:
zipin 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 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<W,A>- Specified by:
lazyZipin interfaceMonad<W,A>- Specified by:
lazyZipin interfaceMonadRec<W,A>- Specified by:
lazyZipin 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:
Maybe,Either
-
discardL
public <B> Writer<W,B> discardL(Applicative<B,Writer<W,?>> appB)
Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB. This is generally useful for sequentially performing side-effects.- Specified by:
discardLin interfaceApplicative<W,A>- Specified by:
discardLin interfaceMonad<W,A>- Specified by:
discardLin interfaceMonadRec<W,A>- Specified by:
discardLin interfaceMonadWriter<W,A,Writer<W,?>>- Type Parameters:
B- the type of the returned Applicative's parameter- Parameters:
appB- the other Applicative- Returns:
- appB
-
discardR
public <B> Writer<W,A> discardR(Applicative<B,Writer<W,?>> appB)
Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative. This is generally useful for sequentially performing side-effects.- Specified by:
discardRin interfaceApplicative<W,A>- Specified by:
discardRin interfaceMonad<W,A>- Specified by:
discardRin interfaceMonadRec<W,A>- Specified by:
discardRin interfaceMonadWriter<W,A,Writer<W,?>>- Type Parameters:
B- the type of appB's parameter- Parameters:
appB- the other Applicative- Returns:
- this Applicative
-
tell
public static <W> Writer<W,Unit> tell(W w)
Construct aWriterfrom an accumulation.- Type Parameters:
W- the accumulation type- Parameters:
w- the accumulation- Returns:
- the
Writer
-
listen
public static <W,A> Writer<W,A> listen(A a)
Construct aWriterfrom a value.- Type Parameters:
W- the accumulation typeA- the value type- Parameters:
a- the output value- Returns:
- the
Writer
-
writer
public static <W,A> Writer<W,A> writer(Tuple2<A,W> aw)
Construct aWriterfrom an accumulation and a value.- Type Parameters:
W- the accumulation typeA- the value type- Parameters:
aw- the output value and accumulation- Returns:
- the
WriterT
-
-