Copyright | (c) 2017 Composewell Technologies |
---|---|
License | BSD3 |
Maintainer | streamly@composewell.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Continuation passing style (CPS) stream implementation. The symbol K
below
denotes a function as well as a Kontinuation.
Synopsis
- newtype Stream m a = MkStream (forall r. State Stream m a -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
- toStreamK :: Stream m a -> Stream m a
- fromStreamK :: Stream m a -> Stream m a
- mkStream :: (forall r. State Stream m a -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r) -> Stream m a
- foldStream :: State Stream m a -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> Stream m a -> m r
- foldStreamShared :: State Stream m a -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> Stream m a -> m r
- foldrM :: (a -> m b -> m b) -> m b -> Stream m a -> m b
- foldrS :: (a -> Stream m b -> Stream m b) -> Stream m b -> Stream m a -> Stream m b
- foldrSShared :: (a -> Stream m b -> Stream m b) -> Stream m b -> Stream m a -> Stream m b
- foldrSM :: Monad m => (m a -> Stream m b -> Stream m b) -> Stream m b -> Stream m a -> Stream m b
- build :: forall m a. (forall b. (a -> b -> b) -> b -> b) -> Stream m a
- buildS :: ((a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a) -> Stream m a
- buildM :: Monad m => (forall r. (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r) -> Stream m a
- buildSM :: Monad m => ((m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a) -> Stream m a
- augmentS :: ((a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
- augmentSM :: Monad m => ((m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
- fromStopK :: StopK m -> Stream m a
- fromYieldK :: YieldK m a -> Stream m a
- consK :: YieldK m a -> Stream m a -> Stream m a
- cons :: a -> Stream m a -> Stream m a
- (.:) :: a -> Stream m a -> Stream m a
- consM :: Monad m => m a -> Stream m a -> Stream m a
- consMBy :: Monad m => (Stream m a -> Stream m a -> Stream m a) -> m a -> Stream m a -> Stream m a
- nil :: Stream m a
- nilM :: Applicative m => m b -> Stream m a
- fromEffect :: Monad m => m a -> Stream m a
- fromPure :: a -> Stream m a
- unfoldr :: (b -> Maybe (a, b)) -> b -> Stream m a
- unfoldrMWith :: Monad m => (m a -> Stream m a -> Stream m a) -> (b -> m (Maybe (a, b))) -> b -> Stream m a
- repeat :: a -> Stream m a
- repeatMWith :: (m a -> t m a -> t m a) -> m a -> t m a
- replicateMWith :: (m a -> Stream m a -> Stream m a) -> Int -> m a -> Stream m a
- fromIndicesMWith :: (m a -> Stream m a -> Stream m a) -> (Int -> m a) -> Stream m a
- iterateMWith :: Monad m => (m a -> Stream m a -> Stream m a) -> (a -> m a) -> m a -> Stream m a
- fromFoldable :: Foldable f => f a -> Stream m a
- fromFoldableM :: (Foldable f, Monad m) => f (m a) -> Stream m a
- mfix :: Monad m => (m a -> Stream m a) -> Stream m a
- uncons :: Applicative m => Stream m a -> m (Maybe (a, Stream m a))
- foldl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> m b
- foldlx' :: forall m a b x. Monad m => (x -> a -> x) -> x -> (x -> b) -> Stream m a -> m b
- drain :: Monad m => Stream m a -> m ()
- null :: Monad m => Stream m a -> m Bool
- tail :: Applicative m => Stream m a -> m (Maybe (Stream m a))
- init :: Applicative m => Stream m a -> m (Maybe (Stream m a))
- conjoin :: Monad m => Stream m a -> Stream m a -> Stream m a
- serial :: Stream m a -> Stream m a -> Stream m a
- map :: (a -> b) -> Stream m a -> Stream m b
- mapMWith :: (m b -> Stream m b -> Stream m b) -> (a -> m b) -> Stream m a -> Stream m b
- mapMSerial :: Monad m => (a -> m b) -> Stream m a -> Stream m b
- unShare :: Stream m a -> Stream m a
- concatMapWith :: (Stream m b -> Stream m b -> Stream m b) -> (a -> Stream m b) -> Stream m a -> Stream m b
- concatMap :: (a -> Stream m b) -> Stream m a -> Stream m b
- bindWith :: (Stream m b -> Stream m b -> Stream m b) -> Stream m a -> (a -> Stream m b) -> Stream m b
- concatPairsWith :: (Stream m b -> Stream m b -> Stream m b) -> (a -> Stream m b) -> Stream m a -> Stream m b
- apWith :: (Stream m b -> Stream m b -> Stream m b) -> Stream m (a -> b) -> Stream m a -> Stream m b
- apSerial :: Stream m (a -> b) -> Stream m a -> Stream m b
- apSerialDiscardFst :: Stream m a -> Stream m b -> Stream m b
- apSerialDiscardSnd :: Stream m a -> Stream m b -> Stream m a
- foldlS :: (Stream m b -> a -> Stream m b) -> Stream m b -> Stream m a -> Stream m b
- reverse :: Stream m a -> Stream m a
- withLocal :: MonadReader r m => (r -> r) -> Stream m a -> Stream m a
The stream type
The type Stream m a
represents a monadic stream of values of type a
constructed using actions in monad m
. It uses stop, singleton and yield
continuations equivalent to the following direct style type:
data Stream m a = Stop | Singleton a | Yield a (Stream m a)
To facilitate parallel composition we maintain a local state in an SVar
that is shared across and is used for synchronization of the streams being
composed.
The singleton case can be expressed in terms of stop and yield but we have
it as a separate case to optimize composition operations for streams with
single element. We build singleton streams in the implementation of pure
for Applicative and Monad, and in lift
for MonadTrans.
fromStreamK :: Stream m a -> Stream m a Source #
foldr/build
mkStream :: (forall r. State Stream m a -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r) -> Stream m a Source #
foldStream :: State Stream m a -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> Stream m a -> m r Source #
Fold a stream by providing a State, stop continuation, a singleton continuation and a yield continuation. The stream will not use the SVar passed via State.
foldStreamShared :: State Stream m a -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> Stream m a -> m r Source #
Fold a stream by providing an SVar, a stop continuation, a singleton continuation and a yield continuation. The stream would share the current SVar passed via the State.
foldrM :: (a -> m b -> m b) -> m b -> Stream m a -> m b Source #
Lazy right fold with a monadic step function.
foldrS :: (a -> Stream m b -> Stream m b) -> Stream m b -> Stream m a -> Stream m b Source #
Lazy right associative fold to a stream.
foldrSShared :: (a -> Stream m b -> Stream m b) -> Stream m b -> Stream m a -> Stream m b Source #
Fold sharing the SVar state within the reconstructed stream
foldrSM :: Monad m => (m a -> Stream m b -> Stream m b) -> Stream m b -> Stream m a -> Stream m b Source #
buildM :: Monad m => (forall r. (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r) -> Stream m a Source #
buildSM :: Monad m => ((m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a) -> Stream m a Source #
augmentS :: ((a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a) -> Stream m a -> Stream m a Source #
augmentSM :: Monad m => ((m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a) -> Stream m a -> Stream m a Source #
Construction
fromYieldK :: YieldK m a -> Stream m a Source #
Make a singleton stream from a callback function. The callback function calls the one-shot yield continuation to yield an element.
consK :: YieldK m a -> Stream m a -> Stream m a Source #
Add a yield function at the head of the stream.
cons :: a -> Stream m a -> Stream m a infixr 5 Source #
Construct a stream by adding a pure value at the head of an existing
stream. For serial streams this is the same as (return a) `consM` r
but
more efficient. For concurrent streams this is not concurrent whereas
consM
is concurrent. For example:
> toList $ 1 `cons` 2 `cons` 3 `cons` nil [1,2,3]
Since: 0.1.0
consMBy :: Monad m => (Stream m a -> Stream m a -> Stream m a) -> m a -> Stream m a -> Stream m a Source #
nilM :: Applicative m => m b -> Stream m a Source #
An empty stream producing a side effect.
> toList (nilM (print "nil")) "nil" []
Pre-release
Generation
fromEffect :: Monad m => m a -> Stream m a Source #
unfoldrMWith :: Monad m => (m a -> Stream m a -> Stream m a) -> (b -> m (Maybe (a, b))) -> b -> Stream m a Source #
repeatMWith :: (m a -> t m a -> t m a) -> m a -> t m a Source #
Like repeatM
but takes a stream cons
operation to combine the actions
in a stream specific manner. A serial cons would repeat the values serially
while an async cons would repeat concurrently.
Pre-release
iterateMWith :: Monad m => (m a -> Stream m a -> Stream m a) -> (a -> m a) -> m a -> Stream m a Source #
fromFoldable :: Foldable f => f a -> Stream m a Source #
Elimination
foldlx' :: forall m a b x. Monad m => (x -> a -> x) -> x -> (x -> b) -> Stream m a -> m b Source #
Strict left fold with an extraction function. Like the standard strict
left fold, but applies a user supplied extraction function (the third
argument) to the folded value at the end. This is designed to work with the
foldl
library. The suffix x
is a mnemonic for extraction.
Note that the accumulator is always evaluated including the initial value.
drain :: Monad m => Stream m a -> m () Source #
drain = foldl' (\_ _ -> ()) () drain = mapM_ (\_ -> return ())
Transformation
serial :: Stream m a -> Stream m a -> Stream m a infixr 6 Source #
Appends two streams sequentially, yielding all elements from the first stream, and then all elements from the second stream.
concatMapWith :: (Stream m b -> Stream m b -> Stream m b) -> (a -> Stream m b) -> Stream m a -> Stream m b Source #
bindWith :: (Stream m b -> Stream m b -> Stream m b) -> Stream m a -> (a -> Stream m b) -> Stream m b Source #
concatPairsWith :: (Stream m b -> Stream m b -> Stream m b) -> (a -> Stream m b) -> Stream m a -> Stream m b Source #
See concatPairsWith
for
documentation.
apWith :: (Stream m b -> Stream m b -> Stream m b) -> Stream m (a -> b) -> Stream m a -> Stream m b Source #
foldlS :: (Stream m b -> a -> Stream m b) -> Stream m b -> Stream m a -> Stream m b Source #
Lazy left fold to a stream.