fold

Given a structure with elements whose type is a Monoid, combine them via the monoid's (<>) operator. This fold is right-associative and lazy in the accumulator. When you need a strict left-associative fold, use foldMap' instead, with id as the map.

Examples

Basic usage:
>>> fold [[1, 2, 3], [4, 5], [6], []]
[1,2,3,4,5,6]
>>> fold $ Node (Leaf (Sum 1)) (Sum 3) (Leaf (Sum 5))
Sum {getSum = 9}
Folds of unbounded structures do not terminate when the monoid's (<>) operator is strict:
>>> fold (repeat Nothing)
* Hangs forever *
Lazy corecursive folds of unbounded structures are fine:
>>> take 12 $ fold $ map (\i -> [i..i+2]) [0..]
[0,1,2,1,2,3,2,3,4,3,4,5]

>>> sum $ take 4000000 $ fold $ map (\i -> [i..i+2]) [0..]
2666668666666
Deprecated: Use Data.IntSet.foldr instead
Deprecated: Use Data.Set.foldr instead
Monoidally combine all values in the stream. Subject to fusion
A strict left fold. Subject to fusion Since 0.3.0
Fold the elements in the set using the given right-associative binary operator. This function is an equivalent of foldr and is present for compatibility only. Please note that fold will be deprecated in the future and removed.
Strict fold of the elements of a Producer
Control.Foldl.purely fold :: Monad m => Fold a b -> Producer a m () -> m b
Tear down a Free Monad using iteration.
Perform a SELECT or other SQL query that is expected to return results. Results are streamed incrementally from the server, and consumed via a left fold. When dealing with small results, it may be simpler (and perhaps faster) to use query instead. This fold is not strict. The stream consumer is responsible for forcing the evaluation of its result to avoid space leaks. This is implemented using a database cursor. As such, this requires a transaction. This function will detect whether or not there is a transaction in progress, and will create a ReadCommitted ReadOnly transaction if needed. The cursor is given a unique temporary name, so the consumer may itself call fold. Exceptions that may be thrown:
  • FormatError: the query string could not be formatted correctly.
  • QueryError: the result contains no columns (i.e. you should be using execute instead of query).
  • ResultError: result conversion failed.
  • SqlError: the postgresql backend returned an error, e.g. a syntax or type error, or an incorrect table or column name.
Apply a strict left Fold to a Foldable container
Apply a strict left Fold to a lazy bytestring
Apply a strict left Fold to lazy text
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
Strict fold of a Stream of elements that preserves the return value. 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 :: Monad m => Fold a b -> Stream (Of a) m r -> m (Of b r)
Thus, specializing a bit:
L.purely S.fold L.sum :: Stream (Of Int) Int r -> m (Of Int r)
mapped (L.purely S.fold L.sum) :: Stream (Stream (Of Int)) IO r -> 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]
([1,2,3],6,6)
([4,5,6],120,15)
([7,8,9],504,24)
([10],10,10)
Combine the elements of a structure using a monoid.
Synonym for ofold
Search a directory recursively, with recursion controlled by a RecursionPredicate. Fold over all files found. Any errors that occur are ignored, with warnings printed to stderr. The fold function is run from "left" to "right", so it should be strict in its left argument to avoid space leaks. If you need a right-to-left fold, use foldr on the result of findWithHandler instead.
Use a Fold to reduce the stream of a's produced by a Shell
Perform a SELECT or other SQL query that is expected to return results. Results are converted and fed into the action callback as they are being retrieved from the database. This allows gives the possibility of processing results in constant space (for instance writing them to disk). Exceptions that may be thrown:
O(n). Fold the association pairs in the map, such that fold f z == foldr f z . assocs. Version: 0.2
Folds a recursive type down to a value, one layer at a time.
>>> :{
let mySum :: [Int] -> Int
mySum = fold $ \case
Nil -> 0
Cons x sumXs -> x + sumXs
:}
>>> mySum [10,11,12]
33
In our running example, one layer consists of an Int and a list of recursive positions. In Tree Int, those recursive positions contain sub-trees of type Tree Int. Since we are working one layer at a time, the Base t a -> a function is not given a Tree Int, but a TreeF Int String. That is, each recursive position contains the String resulting from recursively folding the corresponding sub-tree.
>>> :{
let pprint1 :: Tree Int -> String
pprint1 = fold $ \case
NodeF i [] -> show i
NodeF i ss -> show i ++ ": [" ++ intercalate ", " ss ++ "]"
:}
>>> putStrLn $ pprint1 myTree
0: [1, 2, 3: [31: [311: [3111, 3112]]]]
More generally, the t argument is the recursive value, the a is the final result, and the Base t a -> a function explains how to reduce a single layer full of recursive results down to a result.
fold keeps the return value of the left-folded bytestring. Useful for simultaneous folds over a segmented bytestream.