fold package:io-streams

A left fold over an input stream. The input stream is fully consumed. See foldl. Example:
ghci> Streams.fromList [1..10] >>= Streams.fold (+) 0
55
A side-effecting left fold over an input stream. The input stream is fully consumed. See foldl. Example:
ghci> Streams.fromList [1..10] >>= Streams.foldM (x y -> return (x + y)) 0
55
A variant of foldM suitable for use with composable folds from 'beautiful folding' libraries like the foldl library. The input stream is fully consumed. Example:
ghci> let folds = Foldl.mapM_ print *> Foldl.generalize (liftA2 (,) Foldl.sum Foldl.mean)
ghci> Streams.fromList [1..3::Double] >>= Foldl.impurely Streams.foldM_ folds
1.0
2.0
3.0
(6.0,2.0)
Since 1.3.6.0
A variant of fold suitable for use with composable folds from 'beautiful folding' libraries like the foldl library. The input stream is fully consumed. Example:
ghci> let folds = liftA3 (,,) Foldl.length Foldl.mean Foldl.maximum
ghci> Streams.fromList [1..10::Double] >>= Foldl.purely Streams.fold_ folds is
ghci> (10,5.5,Just 10.0)
Since 1.3.6.0
A side-effecting fold over an InputStream, as a stream transformer. The IO action returned by inputFoldM can be used to fetch and reset the updated seed value. Example:
ghci> is <- Streams.fromList [1, 2, 3::Int]
ghci> (is', getSeed) <- Streams.inputFoldM (\x y -> return (x+y)) 0 is
ghci> Streams.toList is'
[1,2,3]
ghci> getSeed
6
A side-effecting fold over an OutputStream, as a stream transformer. The IO action returned by outputFoldM can be used to fetch and reset the updated seed value. Example:
ghci> is <- Streams.fromList [1, 2, 3::Int]
ghci> (os, getList) <- Streams.listOutputStream
ghci> (os', getSeed) <- Streams.outputFoldM (\x y -> return (x+y)) 0 os
ghci> Streams.connect is os'
ghci> getList
[1,2,3]
ghci> getSeed
6
unfoldM f seed builds an InputStream from successively applying f to the seed value, continuing if f produces Just and halting on Nothing.
ghci> is <- Streams.unfoldM (n -> return $ if n < 3 then Just (n, n + 1) else Nothing) 0
ghci> Streams.toList is
[0,1,2]