HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

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

Consuming Streams of elements

0 declarations

IO Consumers

valuestdoutLn :: Stream (Of Text) IO () %1 -> IO ()
#

Write Strings to stdout using putStrLn; terminates on a broken output pipe (The name and implementation are modelled on the Pipes.Prelude stdoutLn).

>>> withLinearIO $ Control.fmap move $ S.stdoutLn $ S.each $ words "one two three" one two three

valuestdoutLn' :: Stream (Of Text) IO r %1 -> IO r
#

Like stdoutLn but with an arbitrary return value

valueprint :: Show a => Stream (Of a) IO r %1 -> IO r
#

Print the elements of a stream as they arise.

valuetoHandle :: Handle %1 -> Stream (Of Text) RIO r %1 -> RIO (r, Handle)
#

Write a stream to a handle and return the handle.

valuewriteFile :: FilePath -> Stream (Of Text) RIO r %1 -> RIO r
#

Write a stream of text as lines as lines to a file

Basic Pure Consumers

valueeffects :: Monad m => Stream (Of a) m r %1 -> m r
#

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 = id

The similar effects and copy operations in Data.ByteString.Streaming obey the same rules.

valueerase :: Monad m => Stream (Of a) m r %1 -> Stream Identity m r
#

Remove the elements from a stream of values, retaining the structure of layers.

valuedrained
  1. :: (Monad m, Monad (t m), Functor (t m), MonadTrans t)
  2. => t m (Stream (Of a) m r)
  3. -> t m r
#

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
valuemapM_
  1. :: (Consumable b, Monad m)
  2. => a -> m b
  3. -> Stream (Of a) m r
  4. -> m r
#

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

valuefold
  1. :: Monad m
  2. => x -> a -> x
  3. -> x
  4. -> x -> b
  5. -> Stream (Of a) m r
  6. -> m (Of b r)
#

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 r

Here 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)
valuefold_
  1. :: (Monad m, Consumable r)
  2. => x -> a -> x
  3. -> x
  4. -> x -> b
  5. -> Stream (Of a) m r
  6. -> m b
#

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 b
valuefoldM
  1. :: Monad m
  2. => x %1 -> a -> m x
  3. -> m x
  4. -> x %1 -> m b
  5. -> Stream (Of a) m r
  6. -> m (b, r)
#

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 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) :> ()
valuefoldM_
  1. :: (Monad m, Consumable r)
  2. => x %1 -> a -> m x
  3. -> m x
  4. -> x %1 -> m b
  5. -> Stream (Of a) m r
  6. -> m b
#

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 b
valueall :: Monad m => (a -> Bool) -> Stream (Of a) m r %1 -> m (Of Bool r)
#

Note: does not short circuit

valueany :: Monad m => (a -> Bool) -> Stream (Of a) m r %1 -> m (Of Bool r)
#

Note: does not short circuit

valuesum :: (Monad m, Num a) => Stream (Of a) m r %1 -> m (Of a r)
#

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
valuesum_ :: (Monad m, Num a) => Stream (Of a) m () %1 -> m a
#

Fold a Stream of numbers into their sum

valueproduct :: (Monad m, Num a) => Stream (Of a) m r %1 -> m (Of a r)
#

Fold a Stream of numbers into their product with the return value

 mapped product :: Stream (Stream (Of Int)) m r -> Stream (Of Int) m r
valueproduct_ :: (Monad m, Num a) => Stream (Of a) m () %1 -> m a
#

Fold a Stream of numbers into their product

valuehead :: Monad m => Stream (Of a) m r %1 -> m (Of (Maybe a) r)
#

Note that head exhausts the rest of the stream following the first element, performing all monadic effects via effects

valuehead_ :: (Consumable r, Monad m) => Stream (Of a) m r %1 -> m (Maybe a)
#

Note that head exhausts the rest of the stream following the first element, performing all monadic effects via effects

valuelast :: Monad m => Stream (Of a) m r %1 -> m (Of (Maybe a) r)
#
valueelem :: (Monad m, Eq a) => a -> Stream (Of a) m r %1 -> m (Of Bool r)
#
valuenotElem :: (Monad m, Eq a) => a -> Stream (Of a) m r %1 -> m (Of Bool r)
#

Exhaust a stream deciding whether a was an element.

valuelength :: Monad m => Stream (Of a) m r %1 -> m (Of Int r)
#

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
valuelength_ :: (Consumable r, Monad m) => Stream (Of a) m r %1 -> m Int
#

Run a stream, remembering only its length:

>>> runIdentity $ S.length_ (S.each [1..10] :: Stream (Of Int) Identity ())
10
valuetoList :: Monad m => Stream (Of a) m r %1 -> m (Of [a] r)
#

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 r

Like 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"]
valuetoList_ :: Monad m => Stream (Of a) m () %1 -> m [a]
#

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.

valuemconcat :: (Monad m, Monoid w) => Stream (Of w) m r %1 -> m (Of w r)
#

Fold streamed items into their monoidal sum

valueminimum :: (Monad m, Ord a) => Stream (Of a) m r %1 -> m (Of (Maybe a) r)
#
valuemaximum :: (Monad m, Ord a) => Stream (Of a) m r %1 -> m (Of (Maybe a) r)
#
valuefoldrM :: Monad m => (a -> m r %1 -> m r) -> Stream (Of a) m r %1 -> m r
#

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

valuefoldrT
  1. :: (Monad m, MonadTrans t, Monad (t m))
  2. => a -> t m r %1 -> t m r
  3. -> Stream (Of a) m r
  4. -> t m r
#

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