Class IterateT<M extends MonadRec<?,M>, A>
- Type Parameters:
M- the effect typeA- the element type
- All Implemented Interfaces:
Applicative<A, IterateT<M,?>>, Functor<A, IterateT<M, ?>>, Monad<A, IterateT<M, ?>>, MonadBase<M, A, IterateT<?, ?>>, MonadRec<A, IterateT<M, ?>>, MonadT<M, A, IterateT<M, ?>, IterateT<?, ?>>
monad transformer over a co-inductive, singly-linked spine of values embedded in effects. This is
analogous to Haskell's ListT (done right). All append
operations (cons, snoc, etc.) are O(1) space/time
complexity.
Due to its singly-linked embedded design, IterateT is a canonical example of purely-functional streaming
computation. For example, to lazily print all lines from a file descriptor, an initial implementation using
IterateT might take the following form:
String filePath = "/tmp/a_tale_of_two_cities.txt";
IterateT<IO<?>, String> streamLines = IterateT.unfold(
reader -> io(() -> maybe(reader.readLine()).fmap(line -> tuple(line, reader))),
io(() -> Files.newBufferedReader(Paths.get(filePath))));
// iterative read and print lines without retaining references
IO<Unit> printLines = streamLines.forEach(line -> io(() -> System.out.println(line)));
printLines.unsafePerformIO(); // prints "It was the best of times, it was the worst of times, [...]"
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdd an element inside an effect to the front of thisIterateT.discardL(Applicative<B, IterateT<M, ?>> appB) Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB.discardR(Applicative<B, IterateT<M, ?>> appB) Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative.Static factory method for creating an emptyIterateT.Chain dependent computations that may continue or short-circuit based on previous results.Covariantly transmute this functor's parameter using the given mapping function.Monolithically fold the spine of thisIterateTbytrampoliningthe underlying effects (for iterative folding, usetrampolineMdirectly).foldCut(Fn2<? super B, ? super A, ? extends MonadRec<RecursiveResult<B, B>, M>> fn, MonadRec<B, M> acc) Monolithically fold the spine of thisIterateT(with the possibility of early termination) bytrampoliningthe underlying effects (for iterative folding, usetrampolineMdirectly).fromIterator(Iterator<A> as) Given alazyinstance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.Static factory method for creating anIterateTfrom a spine represented by one or more elements.pure(B b) Lift the valuebinto this applicative functor.pureIterateT(Pure<M> pureM) Recover the full structure of the embeddedMonad.runStep()Static factory method for creating anIterateTfrom a single element.Add an element inside an effect to the back of thisIterateT.<C extends Collection<A>, MAS extends MonadRec<C,M>>
MAStoCollection(Fn0<C> cFn0) Force the underlying spine of thisIterateTinto aCollectionof typeCinside the context of the monadic effect, using the providedcFn0to construct the initial instance.trampolineM(Fn1<? super A, ? extends MonadRec<RecursiveResult<A, B>, IterateT<M, ?>>> fn) Given some operation yielding aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.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
-
pureM
-
spine
-
-
Constructor Details
-
IterateT
-
-
Method Details
-
runIterateT
-
runStep
Run a single step of thisIterateT, where a step is the smallest amount of work that could possibly be productive in advancing through theIterateT. Useful for implementing interleaving algorithms that requireIterateTsto yield, emit, or terminate as soon as possible, regardless of whether the next element is readily available.- Type Parameters:
MStep- the witnessed target type of the step- Returns:
- the step
-
cons
-
snoc
-
concat
-
fold
public <B, MB extends MonadRec<B,M>> MB fold(Fn2<? super B, ? super A, ? extends MonadRec<B, M>> fn, MonadRec<B, M> acc) Monolithically fold the spine of thisIterateTbytrampoliningthe underlying effects (for iterative folding, usetrampolineMdirectly).- Type Parameters:
B- the accumulation typeMB- the witnessed target result type- Parameters:
fn- the folding functionacc- the starting accumulation effect- Returns:
- the folded effect result
-
foldCut
public <B, MB extends MonadRec<B,M>> MB foldCut(Fn2<? super B, ? super A, ? extends MonadRec<RecursiveResult<B, B>, M>> fn, MonadRec<B, M> acc) Monolithically fold the spine of thisIterateT(with the possibility of early termination) bytrampoliningthe underlying effects (for iterative folding, usetrampolineMdirectly).- Type Parameters:
B- the accumulation typeMB- the witnessed target result type- Parameters:
fn- the folding functionacc- the starting accumulation effect- Returns:
- the folded effect result
-
forEach
-
lift
- Specified by:
liftin interfaceMonadBase<M extends MonadRec<?,M>, A, IterateT<?, ?>> - Specified by:
liftin interfaceMonadT<M extends MonadRec<?,M>, A, IterateT<M extends MonadRec<?, M>, ?>, IterateT<?, ?>> - Type Parameters:
B- theMonadReccarrier typeN- the argumentMonadRecwitness- Parameters:
nb- the argumentMonadRec- Returns:
- the new
MonadBase
-
trampolineM
public <B> IterateT<M,B> trampolineM(Fn1<? super A, ? extends MonadRec<RecursiveResult<A, B>, IterateT<M, ?>>> 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.
-
flatMap
Chain dependent computations that may continue or short-circuit based on previous results.- Specified by:
flatMapin interfaceMonad<M extends MonadRec<?,M>, A> - Specified by:
flatMapin interfaceMonadRec<M extends MonadRec<?,M>, A> - Specified by:
flatMapin interfaceMonadT<M extends MonadRec<?,M>, A, IterateT<M extends MonadRec<?, M>, ?>, IterateT<?, ?>> - Type Parameters:
B- the resulting monad parameter type- Parameters:
f- the dependent computation over A- Returns:
- the new monad instance
-
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<M extends MonadRec<?,M>, A> - Specified by:
fmapin interfaceFunctor<M extends MonadRec<?,M>, A> - Specified by:
fmapin interfaceMonad<M extends MonadRec<?,M>, A> - Specified by:
fmapin interfaceMonadRec<M extends MonadRec<?,M>, A> - Specified by:
fmapin interfaceMonadT<M extends MonadRec<?,M>, A, IterateT<M extends MonadRec<?, M>, ?>, IterateT<?, ?>> - Type Parameters:
B- the new parameter type- Parameters:
fn- the mapping function- Returns:
- a functor over B (the new parameter type)
-
pure
Lift the valuebinto this applicative functor.- Specified by:
purein interfaceApplicative<M extends MonadRec<?,M>, A> - Specified by:
purein interfaceMonad<M extends MonadRec<?,M>, A> - Specified by:
purein interfaceMonadRec<M extends MonadRec<?,M>, A> - Specified by:
purein interfaceMonadT<M extends MonadRec<?,M>, A, IterateT<M extends MonadRec<?, M>, ?>, IterateT<?, ?>> - Type Parameters:
B- the type of the returned applicative's parameter- Parameters:
b- the value- Returns:
- an instance of this applicative over b
-
toCollection
Force the underlying spine of thisIterateTinto aCollectionof typeCinside the context of the monadic effect, using the providedcFn0to construct the initial instance.Note that this is a fundamentally monolithic operation - meaning that incremental progress is not possible - and as such, calling this on an infinite
IterateTwill result in either heap exhaustion (e.g. in the case oflists) or non-termination (e.g. in the case ofsets).- Type Parameters:
C- theCollectiontypeMAS- the witnessed target type- Parameters:
cFn0- theCollectionconstruction function- Returns:
- the
Listinside of the effect
-
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<M extends MonadRec<?,M>, A> - Specified by:
zipin interfaceMonad<M extends MonadRec<?,M>, A> - Specified by:
zipin interfaceMonadRec<M extends MonadRec<?,M>, A> - Specified by:
zipin interfaceMonadT<M extends MonadRec<?,M>, A, IterateT<M extends MonadRec<?, M>, ?>, IterateT<?, ?>> - Type Parameters:
B- the resulting applicative parameter type- Parameters:
appFn- the other applicative instance- Returns:
- the mapped applicative
-
lazyZip
public <B> Lazy<IterateT<M,B>> lazyZip(Lazy<? extends Applicative<Fn1<? super A, ? extends B>, IterateT<M, ?>>> 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<M extends MonadRec<?,M>, A> - Specified by:
lazyZipin interfaceMonad<M extends MonadRec<?,M>, A> - Specified by:
lazyZipin interfaceMonadRec<M extends MonadRec<?,M>, A> - Specified by:
lazyZipin interfaceMonadT<M extends MonadRec<?,M>, A, IterateT<M extends MonadRec<?, M>, ?>, IterateT<?, ?>> - Type Parameters:
B- the resulting applicative parameter type- Parameters:
lazyAppFn- the lazy other applicative instance- Returns:
- the mapped applicative
- See Also:
-
discardL
Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB. This is generally useful for sequentially performing side-effects.- Specified by:
discardLin interfaceApplicative<M extends MonadRec<?,M>, A> - Specified by:
discardLin interfaceMonad<M extends MonadRec<?,M>, A> - Specified by:
discardLin interfaceMonadRec<M extends MonadRec<?,M>, A> - Specified by:
discardLin interfaceMonadT<M extends MonadRec<?,M>, A, IterateT<M extends MonadRec<?, M>, ?>, IterateT<?, ?>> - Type Parameters:
B- the type of the returned Applicative's parameter- Parameters:
appB- the other Applicative- Returns:
- appB
-
discardR
Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative. This is generally useful for sequentially performing side-effects.- Specified by:
discardRin interfaceApplicative<M extends MonadRec<?,M>, A> - Specified by:
discardRin interfaceMonad<M extends MonadRec<?,M>, A> - Specified by:
discardRin interfaceMonadRec<M extends MonadRec<?,M>, A> - Specified by:
discardRin interfaceMonadT<M extends MonadRec<?,M>, A, IterateT<M extends MonadRec<?, M>, ?>, IterateT<?, ?>> - Type Parameters:
B- the type of appB's parameter- Parameters:
appB- the other Applicative- Returns:
- this Applicative
-
empty
-
singleton
-
iterateT
-
of
@SafeVarargs public static <M extends MonadRec<?,M>, A> IterateT<M,A> of(MonadRec<A, M> ma, MonadRec<A, M>... mas) Static factory method for creating anIterateTfrom a spine represented by one or more elements.- Type Parameters:
M- the effect typeA- the element type- Parameters:
ma- the head elementmas- the tail elements- Returns:
- the
IterateT
-
unfold
public static <M extends MonadRec<?,M>, A, B> IterateT<M,A> unfold(Fn1<? super B, ? extends MonadRec<Maybe<Tuple2<A, B>>, M>> fn, MonadRec<B, M> mb) Lazily unfold anIterateTfrom an unfolding functionfnand a starting seed valuembby successively applyingfnto the latest seed value, producingmaybea value to yield out and the next seed value for the subsequent computation.- Type Parameters:
M- the effect typeA- the element typeB- the seed type- Parameters:
fn- the unfolding functionmb- the starting seed value- Returns:
- the lazily unfolding
IterateT
-
suspended
-
fromIterator
-
pureIterateT
-
liftIterateT
-