Class IterateT<M extends MonadRec<?,M>, A>

java.lang.Object
com.jnape.palatable.lambda.monad.transformer.builtin.IterateT<M,A>
Type Parameters:
M - the effect type
A - 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<?,?>>

public class IterateT<M extends MonadRec<?,M>, A> extends Object implements MonadT<M, A, IterateT<M,?>, IterateT<?,?>>
A 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, [...]"