vector-0.10.0.1: Efficient Arrays

Portability | non-portable
Stability | experimental
Maintainer | Roman Leshchinskiy <rl@cse.unsw.edu.au>
Safe Haskell | None

Data.Vector.Fusion.Stream.Monadic

Contents

- Size hints
- Length
- Construction
- Accessing elements
- Substreams
- Mapping
- Zipping
- Filtering
- Searching
- Folding
- Specialised folds
- Unfolding
- Scans
- Enumerations
- Conversions

Description

Monadic stream combinators.

Synopsis

- data Stream m a = forall s . Stream (s -> m (Step s a)) s Size
- data Step s a
  - = Yield a s
  - | Skip s
  - | Done
- data SPEC
  - = SPEC
  - | SPEC2
- size :: Stream m a -> Size
- sized :: Stream m a -> Size -> Stream m a
- length :: Monad m => Stream m a -> m Int
- null :: Monad m => Stream m a -> m Bool
- empty :: Monad m => Stream m a
- singleton :: Monad m => a -> Stream m a
- cons :: Monad m => a -> Stream m a -> Stream m a
- snoc :: Monad m => Stream m a -> a -> Stream m a
- replicate :: Monad m => Int -> a -> Stream m a
- replicateM :: Monad m => Int -> m a -> Stream m a
- generate :: Monad m => Int -> (Int -> a) -> Stream m a
- generateM :: Monad m => Int -> (Int -> m a) -> Stream m a
- (++) :: Monad m => Stream m a -> Stream m a -> Stream m a
- head :: Monad m => Stream m a -> m a
- last :: Monad m => Stream m a -> m a
- (!!) :: Monad m => Stream m a -> Int -> m a
- (!?) :: Monad m => Stream m a -> Int -> m (Maybe a)
- slice :: Monad m => Int -> Int -> Stream m a -> Stream m a
- init :: Monad m => Stream m a -> Stream m a
- tail :: Monad m => Stream m a -> Stream m a
- take :: Monad m => Int -> Stream m a -> Stream m a
- drop :: Monad m => Int -> Stream m a -> Stream m a
- map :: Monad m => (a -> b) -> Stream m a -> Stream m b
- mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
- mapM_ :: Monad m => (a -> m b) -> Stream m a -> m ()
- trans :: (Monad m, Monad m') => (forall a. m a -> m' a) -> Stream m a -> Stream m' a
- unbox :: Monad m => Stream m (Box a) -> Stream m a
- concatMap :: Monad m => (a -> Stream m b) -> Stream m a -> Stream m b
- flatten :: Monad m => (a -> m s) -> (s -> m (Step s b)) -> Size -> Stream m a -> Stream m b
- indexed :: Monad m => Stream m a -> Stream m (Int, a)
- indexedR :: Monad m => Int -> Stream m a -> Stream m (Int, a)
- zipWithM_ :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> m ()
- zipWithM :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
- zipWith3M :: Monad m => (a -> b -> c -> m d) -> Stream m a -> Stream m b -> Stream m c -> Stream m d
- zipWith4M :: Monad m => (a -> b -> c -> d -> m e) -> Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e
- zipWith5M :: Monad m => (a -> b -> c -> d -> e -> m f) -> Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e -> Stream m f
- zipWith6M :: Monad m => (a -> b -> c -> d -> e -> f -> m g) -> Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e -> Stream m f -> Stream m g
- zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
- zipWith3 :: Monad m => (a -> b -> c -> d) -> Stream m a -> Stream m b -> Stream m c -> Stream m d
- zipWith4 :: Monad m => (a -> b -> c -> d -> e) -> Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e
- zipWith5 :: Monad m => (a -> b -> c -> d -> e -> f) -> Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e -> Stream m f
- zipWith6 :: Monad m => (a -> b -> c -> d -> e -> f -> g) -> Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e -> Stream m f -> Stream m g
- zip :: Monad m => Stream m a -> Stream m b -> Stream m (a, b)
- zip3 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
- zip4 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m (a, b, c, d)
- zip5 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e -> Stream m (a, b, c, d, e)
- zip6 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e -> Stream m f -> Stream m (a, b, c, d, e, f)
- filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
- filterM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
- takeWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
- takeWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
- dropWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
- dropWhileM :: Monad m => (a -> m Bool) -> Stream m a -> 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
- find :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe a)
- findM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe a)
- findIndex :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe Int)
- findIndexM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe Int)
- foldl :: Monad m => (a -> b -> a) -> a -> Stream m b -> m a
- foldlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
- foldl1 :: Monad m => (a -> a -> a) -> Stream m a -> m a
- foldl1M :: Monad m => (a -> a -> m a) -> Stream m a -> m a
- foldM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
- fold1M :: Monad m => (a -> a -> m a) -> Stream m a -> m a
- foldl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> m a
- foldlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
- foldl1' :: Monad m => (a -> a -> a) -> Stream m a -> m a
- foldl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> m a
- foldM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
- fold1M' :: Monad m => (a -> a -> m a) -> Stream m a -> m a
- foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
- foldrM :: Monad m => (a -> b -> m b) -> b -> Stream m a -> m b
- foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m a
- foldr1M :: Monad m => (a -> a -> m a) -> Stream m a -> m a
- and :: Monad m => Stream m Bool -> m Bool
- or :: Monad m => Stream m Bool -> m Bool
- concatMapM :: Monad m => (a -> m (Stream m b)) -> Stream m a -> Stream m b
- unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
- unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Stream m a
- unfoldrN :: Monad m => Int -> (s -> Maybe (a, s)) -> s -> Stream m a
- unfoldrNM :: Monad m => Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
- iterateN :: Monad m => Int -> (a -> a) -> a -> Stream m a
- iterateNM :: Monad m => Int -> (a -> m a) -> a -> Stream m a
- prescanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
- prescanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
- prescanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
- prescanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
- postscanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
- postscanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
- postscanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
- postscanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
- scanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
- scanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
- scanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
- scanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
- scanl1 :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
- scanl1M :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
- scanl1' :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
- scanl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
- enumFromStepN :: (Num a, Monad m) => a -> a -> Int -> Stream m a
- enumFromTo :: (Enum a, Monad m) => a -> a -> Stream m a
- enumFromThenTo :: (Enum a, Monad m) => a -> a -> a -> Stream m a
- toList :: Monad m => Stream m a -> m [a]
- fromList :: Monad m => [a] -> Stream m a
- fromListN :: Monad m => Int -> [a] -> Stream m a
- unsafeFromList :: Monad m => Size -> [a] -> Stream m a

# Documentation

data Stream m a Source

Monadic streams

Constructors

forall s . Stream (s -> m (Step s a)) s Size |

Instances

Monad m => Functor (Stream m) |
Eq a => Eq (Stream Id a) |
Ord a => Ord (Stream Id a) |

data Step s a Source

Result of taking a single step in a stream

Constructors

Yield a s | a new element and a new seed
Skip s | just a new seed
Done | end of stream

data SPEC Source

Constructors

SPEC |
SPEC2 |

# Size hints

size :: Stream m a -> Size Source

`Size` hint of a `Stream`

sized :: Stream m a -> Size -> Stream m aSource

Attach a `Size` hint to a `Stream`

# Length

length :: Monad m => Stream m a -> m Int Source

Length of a `Stream`

null :: Monad m => Stream m a -> m Bool Source

Check if a `Stream` is empty

# Construction

empty :: Monad m => Stream m aSource

Empty `Stream`

singleton :: Monad m => a -> Stream m aSource

Singleton `Stream`

cons :: Monad m => a -> Stream m a -> Stream m aSource

Prepend an element

snoc :: Monad m => Stream m a -> a -> Stream m aSource

Append an element

replicate :: Monad m => Int -> a -> Stream m aSource

Replicate a value to a given length

replicateM :: Monad m => Int -> m a -> Stream m aSource

Yield a `Stream` of values obtained by performing the monadic action the given number of times

generate :: Monad m => Int -> (Int -> a) -> Stream m aSource

generateM :: Monad m => Int -> (Int -> m a) -> Stream m aSource

Generate a stream from its indices

(++) :: Monad m => Stream m a -> Stream m a -> Stream m aSource

Concatenate two `Stream`s

# Accessing elements

head :: Monad m => Stream m a -> m aSource

First element of the `Stream` or error if empty

last :: Monad m => Stream m a -> m aSource

Last element of the `Stream` or error if empty

(!!) :: Monad m => Stream m a -> Int -> m aSource

Element at the given position

(!?) :: Monad m => Stream m a -> Int -> m (Maybe a)Source

Element at the given position or `Nothing` if out of bounds

# Substreams

slice Source

Arguments

:: Monad m |
=> Int | starting index
-> Int | length
-> Stream m a |
-> Stream m a |

Extract a substream of the given length starting at the given position.

init :: Monad m => Stream m a -> Stream m aSource

All but the last element

tail :: Monad m => Stream m a -> Stream m aSource

All but the first element

take :: Monad m => Int -> Stream m a -> Stream m aSource

The first `n` elements

drop :: Monad m => Int -> Stream m a -> Stream m aSource

All but the first `n` elements

# Mapping

map :: Monad m => (a -> b) -> Stream m a -> Stream m bSource

Map a function over a `Stream`

mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m bSource

Map a monadic function over a `Stream`

mapM_ :: Monad m => (a -> m b) -> Stream m a -> m ()Source

Execute a monadic action for each element of the `Stream`

trans :: (Monad m, Monad m') => (forall a. m a -> m' a) -> Stream m a -> Stream m' aSource

Transform a `Stream` to use a different monad

unbox :: Monad m => Stream m (Box a) -> Stream m aSource

concatMap :: Monad m => (a -> Stream m b) -> Stream m a -> Stream m bSource

flatten :: Monad m => (a -> m s) -> (s -> m (Step s b)) -> Size -> Stream m a -> Stream m bSource

Create a `Stream` of values from a `Stream` of streamable things

# Zipping

indexed :: Monad m => Stream m a -> Stream m (Int, a)Source

Pair each element in a `Stream` with its index

indexedR :: Monad m => Int -> Stream m a -> Stream m (Int, a)Source

Pair each element in a `Stream` with its index, starting from the right and counting down

zipWithM_ :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> m ()Source

zipWithM :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m cSource

Zip two `Stream`s with the given monadic function

zipWith3M :: Monad m => (a -> b -> c -> m d) -> Stream m a -> Stream m b -> Stream m c -> Stream m dSource

zipWith4M :: Monad m => (a -> b -> c -> d -> m e) -> Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m eSource

zipWith5M :: Monad m => (a -> b -> c -> d -> e -> m f) -> Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e -> Stream m fSource

zipWith6M :: Monad m => (a -> b -> c -> d -> e -> f -> m g) -> Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e -> Stream m f -> Stream m gSource

zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m cSource

zipWith3 :: Monad m => (a -> b -> c -> d) -> Stream m a -> Stream m b -> Stream m c -> Stream m dSource

zipWith4 :: Monad m => (a -> b -> c -> d -> e) -> Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m eSource

zipWith5 :: Monad m => (a -> b -> c -> d -> e -> f) -> Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e -> Stream m fSource

zipWith6 :: Monad m => (a -> b -> c -> d -> e -> f -> g) -> Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e -> Stream m f -> Stream m gSource

zip :: Monad m => Stream m a -> Stream m b -> Stream m (a, b)Source

zip3 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)Source

zip4 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m (a, b, c, d)Source

zip5 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e -> Stream m (a, b, c, d, e)Source

zip6 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d -> Stream m e -> Stream m f -> Stream m (a, b, c, d, e, f)Source

# Filtering

filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m aSource

Drop elements which do not satisfy the predicate

filterM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m aSource

Drop elements which do not satisfy the monadic predicate

takeWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m aSource

Longest prefix of elements that satisfy the predicate

takeWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m aSource

Longest prefix of elements that satisfy the monadic predicate

dropWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m aSource

Drop the longest prefix of elements that satisfy the predicate

dropWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m aSource

Drop the longest prefix of elements that satisfy the monadic predicate

# Searching

elem :: (Monad m, Eq a) => a -> Stream m a -> m Bool Source

Check whether the `Stream` contains an element

notElem :: (Monad m, Eq a) => a -> Stream m a -> m Bool Source

Inverse of `elem`

find :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe a)Source

Yield `Just` the first element that satisfies the predicate or `Nothing` if no such element exists.

findM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe a)Source

Yield `Just` the first element that satisfies the monadic predicate or `Nothing` if no such element exists.

findIndex :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe Int)Source

Yield `Just` the index of the first element that satisfies the predicate or `Nothing` if no such element exists.

findIndexM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe Int)Source

Yield `Just` the index of the first element that satisfies the monadic predicate or `Nothing` if no such element exists.

# Folding

foldl :: Monad m => (a -> b -> a) -> a -> Stream m b -> m aSource

Left fold

foldlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m aSource

Left fold with a monadic operator

foldl1 :: Monad m => (a -> a -> a) -> Stream m a -> m aSource

Left fold over a non-empty `Stream`

foldl1M :: Monad m => (a -> a -> m a) -> Stream m a -> m aSource

Left fold over a non-empty `Stream` with a monadic operator

foldM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m aSource

Same as `foldlM`

fold1M :: Monad m => (a -> a -> m a) -> Stream m a -> m aSource

Same as `foldl1M`

foldl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> m aSource

Left fold with a strict accumulator

foldlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m aSource

Left fold with a strict accumulator and a monadic operator

foldl1' :: Monad m => (a -> a -> a) -> Stream m a -> m aSource

Left fold over a non-empty `Stream` with a strict accumulator

foldl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> m aSource

Left fold over a non-empty `Stream` with a strict accumulator and a monadic operator

foldM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m aSource

Same as `foldlM'`

fold1M' :: Monad m => (a -> a -> m a) -> Stream m a -> m aSource

Same as `foldl1M'`

foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m bSource

Right fold

foldrM :: Monad m => (a -> b -> m b) -> b -> Stream m a -> m bSource

Right fold with a monadic operator

foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m aSource

Right fold over a non-empty stream

foldr1M :: Monad m => (a -> a -> m a) -> Stream m a -> m aSource

Right fold over a non-empty stream with a monadic operator

# Specialised folds

and :: Monad m => Stream m Bool -> m Bool Source

or :: Monad m => Stream m Bool -> m Bool Source

concatMapM :: Monad m => (a -> m (Stream m b)) -> Stream m a -> Stream m bSource

# Unfolding

unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m aSource

Unfold

unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Stream m aSource

Unfold with a monadic function

unfoldrN :: Monad m => Int -> (s -> Maybe (a, s)) -> s -> Stream m aSource

Unfold at most `n` elements

unfoldrNM :: Monad m => Int -> (s -> m (Maybe (a, s))) -> s -> Stream m aSource

Unfold at most `n` elements with a monadic functions

iterateN :: Monad m => Int -> (a -> a) -> a -> Stream m aSource

Apply function n times to value. Zeroth element is original value.

iterateNM :: Monad m => Int -> (a -> m a) -> a -> Stream m aSource

Apply monadic function n times to value. Zeroth element is original value.

# Scans

prescanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m aSource

Prefix scan

prescanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m aSource

Prefix scan with a monadic operator

prescanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m aSource

Prefix scan with strict accumulator

prescanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m aSource

Prefix scan with strict accumulator and a monadic operator

postscanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m aSource

Suffix scan

postscanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m aSource

Suffix scan with a monadic operator

postscanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m aSource

Suffix scan with strict accumulator

postscanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m aSource

Suffix scan with strict acccumulator and a monadic operator

scanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m aSource

Haskell-style scan

scanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m aSource

Haskell-style scan with a monadic operator

scanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m aSource

Haskell-style scan with strict accumulator

scanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m aSource

Haskell-style scan with strict accumulator and a monadic operator

scanl1 :: Monad m => (a -> a -> a) -> Stream m a -> Stream m aSource

Scan over a non-empty `Stream`

scanl1M :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m aSource

Scan over a non-empty `Stream` with a monadic operator

scanl1' :: Monad m => (a -> a -> a) -> Stream m a -> Stream m aSource

Scan over a non-empty `Stream` with a strict accumulator

scanl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m aSource

Scan over a non-empty `Stream` with a strict accumulator and a monadic operator

# Enumerations

enumFromStepN :: (Num a, Monad m) => a -> a -> Int -> Stream m aSource

Yield a `Stream` of the given length containing the values `x`, `x+y`, `x+y+y` etc.

enumFromTo :: (Enum a, Monad m) => a -> a -> Stream m aSource

Enumerate values

WARNING: This operation can be very inefficient. If at all possible, use `enumFromStepN` instead.

enumFromThenTo :: (Enum a, Monad m) => a -> a -> a -> Stream m aSource

Enumerate values with a given step.

WARNING: This operation is very inefficient. If at all possible, use `enumFromStepN` instead.

# Conversions

toList :: Monad m => Stream m a -> m [a]Source

Convert a `Stream` to a list

fromList :: Monad m => [a] -> Stream m aSource

Convert a list to a `Stream`

fromListN :: Monad m => Int -> [a] -> Stream m aSource

Convert the first `n` elements of a list to a `Stream`

unsafeFromList :: Monad m => Size -> [a] -> Stream m aSource

Convert a list to a `Stream` with the given `Size` hint.