Modulelinear-base-0.4.0Haskell2010
Streaming.Linear.Internal.Consume
This module provides all functions that take input streams but do not return output streams.
- 41 values
- Packagelinear-base-0.4.0
- Exports41
- LanguageHaskell2010
- LicenceMIT
- SourceConsume.hs
Consuming Streams of elements
0 declarationsIO Consumers
Like stdoutLn but with an arbitrary return value
Print the elements of a stream as they arise.
Write a stream to a handle and return the handle.
Write a stream of text as lines as lines to a file
Basic Pure Consumers
Reduce a stream, performing its actions but ignoring its elements.
>>> rest <- S.effects $ S.splitAt 2 $ each' [1..5]
>>> S.print rest
3
4
5
effects should be understood together with copy and is subject to the rules
S.effects . S.copy = id
hoist S.effects . S.copy = idThe similar effects and copy operations in Data.ByteString.Streaming obey the same rules.
Remove the elements from a stream of values, retaining the structure of layers.
Where a transformer returns a stream, run the effects of the stream, keeping the return value. This is usually used at the type
drained :: Control.Monad m => Stream (Of a) m (Stream (Of b) m r) -> Stream (Of a) m r
drained = Control.join . Control.fmap (Control.lift . effects)Here, for example, we split a stream in two places and throw out the middle segment:
>>> rest <- S.print $ S.drained $ S.splitAt 2 $ S.splitAt 5 $ each' [1..7]
1
2
>>> S.print rest
6
7
Reduce a stream to its return value with a monadic action.
>>> S.mapM_ Prelude.print $ each' [1..3]
1
2
3
>>> rest <- S.mapM_ Prelude.print $ S.splitAt 3 $ each' [1..10]
1
2
3
>>> S.sum rest
49 :> ()
Folds
Strict fold of a Stream of elements that preserves the return value.
This does not short circuit and all effects are performed.
The third parameter will often be id where a fold is written by hand:
>>> S.fold (+) 0 id $ each' [1..10]
55 :> ()
>>> S.fold (*) 1 id $ S.fold (+) 0 id $ S.copy $ each' [1..10]
3628800 :> (55 :> ())
It can be used to replace a standard Haskell type with one more suited to
writing a strict accumulation function. It is also crucial to the
Applicative instance for Control.Foldl.Fold We can apply such a fold
purely
Control.Foldl.purely S.fold :: Control.Monad m => Fold a b -> Stream (Of a) m r %1-> m (Of b r)Thus, specializing a bit:
L.purely S.fold L.sum :: Stream (Of Int) Int r %1-> m (Of Int r)
mapped (L.purely S.fold L.sum) :: Stream (Stream (Of Int)) IO r %1-> Stream (Of Int) IO rHere we use the Applicative instance for Control.Foldl.Fold to
stream three-item segments of a stream together with their sums and products.
>>> S.print $ mapped (L.purely S.fold (liftA3 (,,) L.list L.product L.sum)) $ chunksOf 3 $ each' 1..10
([4,5,6],120,15)
([7,8,9],504,24)
([10],10,10)
Strict fold of a Stream of elements, preserving only the result of the fold, not
the return value of the stream. This does not short circuit and all effects
are performed. The third parameter will often be id where a fold
is written by hand:
>>> S.fold_ (+) 0 id $ each [1..10]
55
It can be used to replace a standard Haskell type with one more suited to
writing a strict accumulation function. It is also crucial to the
Applicative instance for Control.Foldl.Fold
Control.Foldl.purely fold :: Control.Monad m => Fold a b -> Stream (Of a) m () %1-> m bStrict, monadic fold of the elements of a Stream (Of a)
Control.Foldl.impurely foldM :: Control.Monad m => FoldM a b -> Stream (Of a) m r %1-> m (b, r)Thus to accumulate the elements of a stream as a vector, together with a random element we might write:
>>> L.impurely S.foldM (liftA2 (,) L.vectorM L.random) $ each' [1..10::Int] :: IO (Of (Vector Int, Maybe Int) ())
([1,2,3,4,5,6,7,8,9,10],Just 9) :> ()
Strict, monadic fold of the elements of a Stream (Of a)
Control.Foldl.impurely foldM_ :: Control.Monad m => FoldM a b -> Stream (Of a) m () %1-> m bNote: does not short circuit
Note: does not short circuit
Note: does not short circuit
Note: does not short circuit
Fold a Stream of numbers into their sum with the return value
mapped S.sum :: Stream (Stream (Of Int)) m r %1-> Stream (Of Int) m r>>> S.sum $ each' [1..10]
55 :> ()
>>> (n :> rest) <- S.sum $ S.splitAt 3 $ each' [1..10]
>>> System.IO.print n
6
>>> (m :> rest') <- S.sum $ S.splitAt 3 rest
>>> System.IO.print m
15
>>> S.print rest'
7
8
9
10
Fold a Stream of numbers into their sum
Fold a Stream of numbers into their product with the return value
mapped product :: Stream (Stream (Of Int)) m r -> Stream (Of Int) m rFold a Stream of numbers into their product
Note that head exhausts the rest of the stream following the
first element, performing all monadic effects via effects
Note that head exhausts the rest of the stream following the
first element, performing all monadic effects via effects
Exhaust a stream deciding whether a was an element.
Run a stream, keeping its length and its return value.
>>> S.print $ mapped S.length $ chunksOf 3 $ S.each' [1..10]
3
3
3
1
Run a stream, remembering only its length:
>>> runIdentity $ S.length_ (S.each [1..10] :: Stream (Of Int) Identity ())
10
Convert an effectful Stream into a list alongside the return value
mapped toList :: Stream (Stream (Of a) m) m r %1-> Stream (Of [a]) m rLike toList_, toList breaks streaming; unlike toList_ it preserves the return value
and thus is frequently useful with e.g. mapped
>>> S.print $ mapped S.toList $ chunksOf 3 $ each' [1..9]
[1,2,3]
[4,5,6]
[7,8,9]
>>> S.print $ mapped S.toList $ chunksOf 2 $ S.replicateM 4 getLine
sEnter
tEnter
["s","t"]
uEnter
vEnter
["u","v"]
Convert an effectful Stream (Of a) into a list of as
Note: Needless to say, this function does not stream properly.
It is basically the same as Prelude mapM which, like replicateM,
sequence and similar operations on traversable containers
is a leading cause of space leaks.
Fold streamed items into their monoidal sum
A natural right fold for consuming a stream of elements.
See also the more general iterT in the Streaming module and the
still more general destroy
A natural right fold for consuming a stream of elements.
See also the more general iterTM in the Streaming module
and the still more general destroy
foldrT (\a p -> Streaming.yield a >> p) = id