streamly-0.1.2: Beautiful Streaming, Concurrent and Reactive Composition

Copyright(c) 2017 Harendra Kumar
LicenseBSD3
Maintainerharendra.kumar@gmail.com
Stabilityexperimental
PortabilityGHC
Safe HaskellNone
LanguageHaskell2010

Streamly.Prelude

Contents

Description

 

Synopsis

Construction

nil :: Streaming t => t m a Source #

Represesnts an empty stream just like [] represents an empty list.

cons :: Streaming t => a -> t m a -> t m a infixr 5 Source #

Constructs a stream by adding a pure value at the head of an existing stream, just like : constructs lists. For example:

> let stream = 1 `cons` 2 `cons` 3 `cons` nil
> (toList . serially) stream
[1,2,3]

(.:) :: Streaming t => a -> t m a -> t m a infixr 5 Source #

Operator equivalent of cons so that you can construct a stream of pure values more succinctly like this:

> let stream = 1 .: 2 .: 3 .: nil
> (toList . serially) stream
[1,2,3]

.: constructs a stream just like : constructs a list.

Also note that another equivalent way of building streams from pure values is:

> let stream = pure 1 <> pure 2 <> pure 3
> (toList . serially) stream
[1,2,3]

In the first method we construct a stream by adding one element at a time. In the second method we first construct singleton streams using pure and then compose all those streams together using the Semigroup style composition of streams. The former method is a bit more efficient than the latter.

unfoldr :: Streaming t => (b -> Maybe (a, b)) -> b -> t m a Source #

Build a Stream by unfolding pure steps starting from a seed.

unfoldrM :: (Streaming t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a Source #

Build a Stream by unfolding monadic steps starting from a seed.

each :: (Streaming t, Foldable f) => f a -> t m a Source #

Same as foldWith (<>) but more efficient.

iterate :: Streaming t => (a -> a) -> a -> t m a Source #

Iterate a pure function from a seed value, streaming the results forever

iterateM :: (Streaming t, Monad m) => (a -> m a) -> a -> t m a Source #

Iterate a monadic function from a seed value, streaming the results forever

Elimination

General Folds

foldr :: (Streaming t, Monad m) => (a -> b -> b) -> b -> t m a -> m b Source #

Right fold.

foldrM :: Streaming t => (a -> m b -> m b) -> m b -> t m a -> m b Source #

Right fold with a monadic step function. See toList for an example use.

scan :: Streaming t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b Source #

Scan left. A strict left fold which accumulates the result of its reduction steps inside a stream, from left.

foldl :: (Streaming t, Monad m) => (x -> a -> x) -> x -> (x -> b) -> t m a -> m b Source #

Strict left fold. This is typed to work with the foldl package. To use it normally just pass id as the third argument.

foldlM :: (Streaming t, Monad m) => (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b Source #

Strict left fold, with monadic step function. This is typed to work with the foldl package. To use directly pass id as the third argument.

uncons :: (Streaming t, Monad m) => t m a -> m (Maybe (a, t m a)) Source #

Decompose a stream into its head and tail. If the stream is empty, returns Nothing. If the stream is non-empty, returns 'Just (a, ma)', where a is the head of the stream and ma its tail.

Special Folds

toList :: (Streaming t, Monad m) => t m a -> m [a] Source #

Convert a stream into a list in the underlying monad.

all :: (Streaming t, Monad m) => (a -> Bool) -> t m a -> m Bool Source #

Determine whether all elements of a stream satisfy a predicate.

any :: (Streaming t, Monad m) => (a -> Bool) -> t m a -> m Bool Source #

Determine whether any of the elements of a stream satisfy a predicate.

head :: (Streaming t, Monad m) => t m a -> m (Maybe a) Source #

Extract the first element of the stream, if any.

tail :: (Streaming t, Monad m) => t m a -> m (Maybe (t m a)) Source #

Extract all but the first element of the stream, if any.

last :: (Streaming t, Monad m) => t m a -> m (Maybe a) Source #

Extract the last element of the stream, if any.

null :: (Streaming t, Monad m) => t m a -> m Bool Source #

Determine whether the stream is empty.

length :: (Streaming t, Monad m) => t m a -> m Int Source #

Determine the length of the stream.

elem :: (Streaming t, Monad m, Eq a) => a -> t m a -> m Bool Source #

Determine whether an element is present in the stream.

notElem :: (Streaming t, Monad m, Eq a) => a -> t m a -> m Bool Source #

Determine whether an element is not present in the stream.

reverse :: Streaming t => t m a -> t m a Source #

Returns the elements of the stream in reverse order. The stream must be finite.

maximum :: (Streaming t, Monad m, Ord a) => t m a -> m (Maybe a) Source #

Determine the maximum element in a stream.

minimum :: (Streaming t, Monad m, Ord a) => t m a -> m (Maybe a) Source #

Determine the minimum element in a stream.

sum :: (Streaming t, Monad m, Num a) => t m a -> m a Source #

Determine the sum of all elements of a stream of numbers

product :: (Streaming t, Monad m, Num a) => t m a -> m a Source #

Determine the product of all elements of a stream of numbers

Filtering

filter :: Streaming t => (a -> Bool) -> t m a -> t m a Source #

Include only those elements that pass a predicate.

take :: Streaming t => Int -> t m a -> t m a Source #

Take first n elements from the stream and discard the rest.

takeWhile :: Streaming t => (a -> Bool) -> t m a -> t m a Source #

End the stream as soon as the predicate fails on an element.

drop :: Streaming t => Int -> t m a -> t m a Source #

Discard first n elements from the stream and take the rest.

dropWhile :: Streaming t => (a -> Bool) -> t m a -> t m a Source #

Drop elements in the stream as long as the predicate succeeds and then take the rest of the stream.

Transformation

mapM :: (Streaming t, Monad m) => (a -> m b) -> t m a -> t m b Source #

Replace each element of the stream with the result of a monadic action applied on the element.

mapM_ :: (Streaming t, Monad m) => (a -> m b) -> t m a -> m () Source #

Apply a monadic action to each element of the stream and discard the output of the action.

sequence :: (Streaming t, Monad m) => t m (m a) -> t m a Source #

Reduce a stream of monadic actions to a stream of the output of those actions.

replicateM :: (Streaming t, Monad m) => Int -> m a -> t m a Source #

Generate a stream by performing an action n times.

Zipping

zipWith :: Streaming t => (a -> b -> c) -> t m a -> t m b -> t m c Source #

Zip two streams serially using a pure zipping function.

zipWithM :: Streaming t => (a -> b -> t m c) -> t m a -> t m b -> t m c Source #

Zip two streams serially using a monadic zipping function.

zipAsyncWith :: (Streaming t, MonadAsync m) => (a -> b -> c) -> t m a -> t m b -> t m c Source #

Zip two streams asyncly (i.e. both the elements being zipped are generated concurrently) using a pure zipping function.

zipAsyncWithM :: (Streaming t, MonadAsync m) => (a -> b -> t m c) -> t m a -> t m b -> t m c Source #

Zip two streams asyncly (i.e. both the elements being zipped are generated concurrently) using a monadic zipping function.

IO

fromHandle :: (Streaming t, MonadIO m) => Handle -> t m String Source #

Read lines from an IO Handle into a stream of Strings.

toHandle :: (Streaming t, MonadIO m) => Handle -> t m String -> m () Source #

Write a stream of Strings to an IO Handle.