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.
import qualified Streamly.Internal.Data.Stream.StreamK as K
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)
- mkStream :: (forall r. State Stream m a -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r) -> Stream m a
- nil :: Stream m a
- nilM :: Applicative m => m b -> Stream m a
- cons :: a -> Stream m a -> Stream m a
- (.:) :: a -> Stream m a -> 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
- unShare :: Stream m a -> Stream m a
- uncons :: Applicative m => Stream m a -> m (Maybe (a, Stream m a))
- unfoldr :: (b -> Maybe (a, b)) -> b -> Stream m a
- unfoldrM :: Monad m => (b -> m (Maybe (a, b))) -> b -> Stream m a
- repeat :: a -> Stream m a
- repeatM :: Monad m => m a -> Stream m a
- replicate :: Int -> a -> Stream m a
- replicateM :: Monad m => Int -> m a -> Stream m a
- fromIndices :: (Int -> a) -> Stream m a
- fromIndicesM :: Monad m => (Int -> m a) -> Stream m a
- iterate :: (a -> a) -> a -> Stream m a
- iterateM :: Monad m => (a -> m a) -> m a -> Stream m a
- fromPure :: a -> Stream m a
- fromEffect :: Monad m => m a -> Stream m a
- fromFoldable :: Foldable f => f a -> Stream m a
- fromList :: [a] -> Stream m a
- foldrS :: (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
- buildS :: ((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
- foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
- foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m (Maybe a)
- foldrM :: (a -> m b -> m b) -> m b -> Stream m a -> m b
- foldrT :: (Monad m, Monad (s m), MonadTrans s) => (a -> s m b -> s m b) -> s m b -> Stream m a -> s m b
- foldl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> m b
- foldlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> m b
- foldlS :: (Stream m b -> a -> Stream m b) -> Stream m b -> Stream m a -> Stream m b
- foldlT :: (Monad m, Monad (s m), MonadTrans s) => (s m b -> a -> s m b) -> s m b -> Stream m a -> s m b
- foldlx' :: forall m a b x. Monad m => (x -> a -> x) -> x -> (x -> b) -> Stream m a -> m b
- foldlMx' :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> m b
- fold :: Monad m => Fold m a b -> Stream m a -> m b
- drain :: Monad m => Stream m a -> m ()
- null :: Monad m => Stream m a -> m Bool
- head :: Monad m => Stream m a -> m (Maybe a)
- tail :: Applicative m => Stream m a -> m (Maybe (Stream m a))
- init :: Applicative m => Stream m a -> m (Maybe (Stream m a))
- elem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
- notElem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
- all :: Monad m => (a -> Bool) -> Stream m a -> m Bool
- any :: Monad m => (a -> Bool) -> Stream m a -> m Bool
- last :: Monad m => Stream m a -> m (Maybe a)
- minimum :: (Monad m, Ord a) => Stream m a -> m (Maybe a)
- minimumBy :: Monad m => (a -> a -> Ordering) -> Stream m a -> m (Maybe a)
- maximum :: (Monad m, Ord a) => Stream m a -> m (Maybe a)
- maximumBy :: Monad m => (a -> a -> Ordering) -> Stream m a -> m (Maybe a)
- findIndices :: (a -> Bool) -> Stream m a -> Stream m Int
- lookup :: (Monad m, Eq a) => a -> Stream m (a, b) -> m (Maybe b)
- findM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe a)
- find :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe a)
- (!!) :: Monad m => Stream m a -> Int -> m (Maybe a)
- mapM_ :: Monad m => (a -> m b) -> Stream m a -> m ()
- toList :: Monad m => Stream m a -> m [a]
- hoist :: (Monad m, Monad n) => (forall x. m x -> n x) -> Stream m a -> Stream n a
- scanl' :: (b -> a -> b) -> b -> Stream m a -> Stream m b
- scanlx' :: (x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
- filter :: (a -> Bool) -> Stream m a -> Stream m a
- take :: Int -> Stream m a -> Stream m a
- takeWhile :: (a -> Bool) -> Stream m a -> Stream m a
- drop :: Int -> Stream m a -> Stream m a
- dropWhile :: (a -> Bool) -> Stream m a -> Stream m a
- map :: (a -> b) -> Stream m a -> Stream m b
- mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
- sequence :: Monad m => Stream m (m a) -> Stream m a
- intersperseM :: Monad m => m a -> Stream m a -> Stream m a
- intersperse :: Monad m => a -> Stream m a -> Stream m a
- insertBy :: (a -> a -> Ordering) -> a -> Stream m a -> Stream m a
- deleteBy :: (a -> a -> Bool) -> a -> Stream m a -> Stream m a
- reverse :: Stream m a -> Stream m a
- mapMaybe :: (a -> Maybe b) -> Stream m a -> Stream m b
- zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
- zipWithM :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
- mergeBy :: (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
- mergeByM :: Monad m => (a -> a -> m Ordering) -> Stream m a -> 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
- the :: (Eq a, Monad m) => Stream m a -> m (Maybe a)
- serial :: Stream m a -> Stream m a -> Stream m a
- consM :: Monad m => m a -> Stream m a -> Stream m a
- withLocal :: MonadReader r m => (r -> r) -> Stream m a -> Stream m a
- mfix :: Monad m => (m a -> 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.
Construction Primitives
mkStream :: (forall r. State Stream m a -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r) -> 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
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
Elimination Primitives
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.
Transformation Primitives
Deconstruction
Generation
Unfolds
Specialized Generation
fromIndices :: (Int -> a) -> Stream m a Source #
Conversions
fromEffect :: Monad m => m a -> Stream m a Source #
fromFoldable :: Foldable f => f a -> Stream m a Source #
foldr/build
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.
foldrSM :: Monad m => (m a -> Stream m b -> Stream m b) -> Stream m b -> Stream m a -> Stream m b Source #
augmentS :: ((a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a) -> Stream m a -> Stream m a Source #
Elimination
General Folds
foldrM :: (a -> m b -> m b) -> m b -> Stream m a -> m b Source #
Lazy right fold with a monadic step function.
foldrT :: (Monad m, Monad (s m), MonadTrans s) => (a -> s m b -> s m b) -> s m b -> Stream m a -> s m b Source #
Right associative fold to an arbitrary transformer monad.
foldlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> m b Source #
Like foldl'
but with a monadic step function.
foldlS :: (Stream m b -> a -> Stream m b) -> Stream m b -> Stream m a -> Stream m b Source #
Lazy left fold to a stream.
foldlT :: (Monad m, Monad (s m), MonadTrans s) => (s m b -> a -> s m b) -> s m b -> Stream m a -> s m b Source #
Lazy left fold to an arbitrary transformer monad.
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.
foldlMx' :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> m b Source #
Like foldx
, but with a monadic step function.
Specialized Folds
drain :: Monad m => Stream m a -> m () Source #
drain = foldl' (\_ _ -> ()) () drain = mapM_ (\_ -> return ())
last :: Monad m => Stream m a -> m (Maybe a) Source #
Extract the last element of the stream, if any.
Map and Fold
mapM_ :: Monad m => (a -> m b) -> Stream m a -> m () Source #
Apply a monadic action to each element of the stream and discard the output of the action.
Conversions
Transformation
By folding (scans)
Filtering
Mapping
Inserting
Deleting
Reordering
Map and Filter
Zipping
zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c Source #
Zip two streams serially using a pure zipping function.
Since: 0.1.0
zipWithM :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c Source #
Zip two streams serially using a monadic zipping function.
Since: 0.1.0
Merging
Nesting
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 #
Transformation comprehensions
Semigroup Style Composition
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.