Copyright | (c) 2021 Composewell Technologies |
---|---|
License | BSD-3-Clause |
Maintainer | streamly@composewell.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | None |
Language | Haskell2010 |
A Producer
is an Unfold
with an extract
function added to extract
the state. It is more powerful but less general than an Unfold.
A Producer
represents steps of a loop generating a sequence of elements.
While unfolds are closed representation of imperative loops with some opaque
internal state, producers are open loops with the state being accessible to
the user.
Unlike an unfold, which runs a loop till completion, a producer can be stopped in the middle, its state can be extracted, examined, changed, and then it can be resumed later from the stopped state.
A producer can be used in places where a CPS stream would otherwise be needed, because the state of the loop can be passed around. However, it can be much more efficient than CPS because it allows stream fusion and unecessary function calls can be avoided.
Synopsis
- data Producer m a b = forall s. Producer (s -> m (Step s b)) (a -> m s) (s -> m a)
- simplify :: Producer m a b -> Unfold m a b
- nil :: Monad m => Producer m a b
- nilM :: Monad m => (a -> m c) -> Producer m a b
- unfoldrM :: Monad m => (a -> m (Maybe (b, a))) -> Producer m a b
- fromStreamD :: Monad m => Producer m (Stream m a) a
- fromList :: Monad m => Producer m [a] a
- data NestedLoop s1 s2
- concat :: Monad m => Producer m a b -> Producer m b c -> Producer m (NestedLoop a b) c
Documentation
A Producer m a b
is a generator of a stream of values of type b
from a
seed of type a
in Monad
m
.
Pre-release
Converting
Producers
fromStreamD :: Monad m => Producer m (Stream m a) a Source #
Convert a StreamD stream into a producer.
Pre-release
fromList :: Monad m => Producer m [a] a Source #
Convert a list of pure values to a Stream
Pre-release