2-- Module : Streamly.Data.Fold
3-- Copyright : (c) 2019 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
9-- A 'Fold' is a sink or a consumer of a stream of values. The 'Fold' type
10-- consists of an accumulator and an effectful action that absorbs a value into
13-- >>> import qualified Streamly.Data.Fold as Fold
14-- >>> import qualified Streamly.Prelude as Stream
16-- For example, a 'sum' Fold represents adding the input to the accumulated
17-- sum. A fold driver e.g. 'Streamly.Prelude.fold' pushes values from a stream
18-- to the 'Fold' one at a time, reducing the stream to a single value.
20-- >>> Stream.fold Fold.sum $ Stream.fromList [1..100]
23-- Conceptually, a 'Fold' is a data type that can mimic a strict left fold
24-- ('Data.List.foldl') as well as lazy right fold ('Prelude.foldr'). The above
25-- example is similar to a left fold using @(+)@ as the step and @0@ as the
26-- initial value of the accumulator:
28-- >>> Data.List.foldl' (+) 0 [1..100]
31-- 'Fold's have an early termination capability e.g. the 'head' fold would
32-- terminate on an infinite stream:
34-- >>> Stream.fold Fold.head $ Stream.fromList [1..]
37-- The above example is similar to the following right fold:
39-- >>> Prelude.foldr (\x _ -> Just x) Nothing [1..]
42-- 'Fold's can be combined together using combinators. For example, to create a
43-- fold that sums first two elements in a stream:
45-- >>> sumTwo = Fold.take 2 Fold.sum
46-- >>> Stream.fold sumTwo $ Stream.fromList [1..100]
49-- Folds can be combined to run in parallel on the same input. For example, to
50-- compute the average of numbers in a stream without going through the stream
53-- >>> avg = Fold.teeWith (/) Fold.sum (fmap fromIntegral Fold.length)
54-- >>> Stream.fold avg $ Stream.fromList [1.0..100.0]
57-- Folds can be combined so as to partition the input stream over multiple
58-- folds. For example, to count even and odd numbers in a stream:
60-- >>> split n = if even n then Left n else Right n
61-- >>> stream = Stream.map split $ Stream.fromList [1..100]
62-- >>> countEven = fmap (("Even " ++) . show) Fold.length
63-- >>> countOdd = fmap (("Odd " ++) . show) Fold.length
64-- >>> f = Fold.partition countEven countOdd
65-- >>> Stream.fold f stream
66-- ("Even 50","Odd 50")
68-- Terminating folds can be combined to parse the stream serially such that the
69-- first fold consumes the input until it terminates and the second fold
70-- consumes the rest of the input until it terminates:
72-- >>> f = Fold.serialWith (,) (Fold.take 8 Fold.toList) (Fold.takeEndBy (== '\n') Fold.toList)
73-- >>> Stream.fold f $ Stream.fromList "header: hello\n"
74-- ("header: ","hello\n")
76-- A 'Fold' can be applied repeatedly on a stream to transform it to a stream
77-- of fold results. To split a stream on newlines:
79-- >>> f = Fold.takeEndBy (== '\n') Fold.toList
80-- >>> Stream.toList $ Stream.foldMany f $ Stream.fromList "Hello there!\nHow are you\n"
81-- ["Hello there!\n","How are you\n"]
83-- Similarly, we can split the input of a fold too:
85-- >>> Stream.fold (Fold.many f Fold.toList) $ Stream.fromList "Hello there!\nHow are you\n"
86-- ["Hello there!\n","How are you\n"]
88-- Please see "Streamly.Internal.Data.Fold" for additional @Pre-release@
93-- We can often use streams or folds to achieve the same goal. However, streams
94-- allow efficient composition of producers (e.g. 'Streamly.Prelude.serial' or
95-- 'Streamly.Prelude.mergeBy') whereas folds allow efficient composition of
96-- consumers (e.g. 'serialWith', 'partition' or 'teeWith').
98-- Streams are producers, transformations on streams happen on the output side:
100-- >>> f = Stream.sum . Stream.map (+1) . Stream.filter odd
101-- >>> f $ Stream.fromList [1..100]
104-- Folds are stream consumers with an input stream and an output value, stream
105-- transformations on folds happen on the input side:
107-- >>> f = Fold.filter odd $ Fold.lmap (+1) $ Fold.sum
108-- >>> Stream.fold f $ Stream.fromList [1..100]
111-- Notice the composition by @.@ vs @$@ and the order of operations in the
112-- above examples, the difference is due to output vs input side
115module Streamly.Data.Fold
128 -- | Folds that never terminate, these folds are much like strict left
129 -- folds. 'mconcat' is the fundamental accumulator. All other accumulators
130 -- can be expressed in terms of 'mconcat' using a suitable Monoid. Instead
131 -- of writing folds we could write Monoids and turn them into folds.
154 , rollingHashWithSalt
160 -- ** Terminating Folds
161 -- | These are much like lazy right folds.
178 -- | Combinators are modifiers of folds. In the type @Fold m a b@, @a@ is
179 -- the input type and @b@ is the output type. Transformations can be
180 -- applied either on the input side or on the output side. Therefore,
181 -- combinators are of one of the following general shapes:
183 -- * @... -> Fold m a b -> Fold m c b@ (input transformation)
184 -- * @... -> Fold m a b -> Fold m a c@ (output transformation)
186 -- Output transformations are also known as covariant transformations, and
187 -- input transformations are also known as contravariant transformations.
188 -- The input side transformations are more interesting for folds. Most of
189 -- the following sections describe the input transformation operations on a
190 -- fold. The names and signatures of the operations are consistent with
191 -- corresponding operations in "Streamly.Prelude". When an operation makes
192 -- sense on both input and output side we use the prefix @l@ (for left) for
193 -- input side operations and the prefix @r@ (for right) for output side
196 -- ** Mapping on output
197 -- | The 'Functor' instance of a fold maps on the output of the fold:
199 -- >>> Stream.fold (fmap show Fold.sum) (Stream.enumerateFromTo 1 100)
204 -- ** Mapping on Input
212 -- -- ** Mapping Filters
225 -- ** Parallel Distribution
226 -- | For applicative composition using distribution see
227 -- "Streamly.Internal.Data.Fold.Tee".
234 -- | Direct items in the input stream to different folds using a binary
257 hiding (filter, drop, dropWhile, take, takeWhile, zipWith, foldr,
258 foldl, map, mapM_, sequence, all, any, sum, product, elem,
259 notElem, maximum, minimum, head, last, tail, length, null,
260 reverse, iterate, init, and, or, lookup, foldr1, (!!),
261 scanl, scanl1, replicate, concatMap, mconcat, foldMap, unzip,
262 span, splitAt, break, mapM)
264import Streamly.Internal.Data.Fold
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list