foldl
Composable, streaming, and efficient left folds
| LTS Haskell 24.18: | 1.4.18@rev:1 | 
| Stackage Nightly 2025-11-04: | 1.4.18@rev:1 | 
| Latest on Hackage: | 1.4.18@rev:1 | 
foldl-1.4.18@sha256:aa42a0d8db37909c2bf69861447b9f40b4993166f32a981f6eefbac503d151c7,2793Module documentation for 1.4.18
foldl
Use this foldl library when you want to compute multiple folds over a
collection in one pass over the data without space leaks.
For example, suppose that you want to simultaneously compute the sum of the list and the length of the list. Many Haskell beginners might write something like this:
sumAndLength :: Num a => [a] -> (a, Int)
sumAndLength xs = (sum xs, length xs)
However, this solution will leak space because it goes over the list in two
passes.  If you demand the result of sum the Haskell runtime will materialize
the entire list.  However, the runtime cannot garbage collect the list because
the list is still required for the call to length.
Usually people work around this by hand-writing a strict left fold that looks something like this:
{-# LANGUAGE BangPatterns #-}
import Data.List (foldl')
sumAndLength :: Num a => [a] -> (a, Int)
sumAndLength xs = foldl' step (0, 0) xs
  where
    step (x, y) n = (x + n, y + 1)
That now goes over the list in one pass, but will still leak space because the
tuple is not strict in both fields!  You have to define a strict Pair type to
fix this:
{-# LANGUAGE BangPatterns #-}
import Data.List (foldl')
data Pair a b = Pair !a !b
sumAndLength :: Num a => [a] -> (a, Int)
sumAndLength xs = done (foldl' step (Pair 0 0) xs)
  where
    step (Pair x y) n = Pair (x + n) (y + 1)
    done (Pair x y) = (x, y)
However, this is not satisfactory because you have to reimplement the guts of every fold that you care about and also define a custom strict data type for your fold. Hand-writing the step function, accumulator, and strict data type for every fold that you want to use gets tedious fast. For example, implementing something like reservoir sampling over and over is very error prone.
What if you just stored the step function and accumulator for each individual fold and let some high-level library do the combining for you? That’s exactly what this library does! Using this library you can instead write:
import qualified Control.Foldl as Fold
sumAndLength :: Num a => [a] -> (a, Int)
sumAndLength xs = Fold.fold ((,) <$> Fold.sum <*> Fold.length) xs
-- or, more concisely:
sumAndLength = Fold.fold ((,) <$> Fold.sum <*> Fold.length)
To see how this works, the Fold.sum value is just a datatype storing the step
function and the starting state (and a final extraction function):
sum :: Num a => Fold a a
sum = Fold (+) 0 id
Same thing for the Fold.length value:
length :: Fold a Int
length = Fold (\n _ -> n + 1) 0 id
… and the Applicative operators combine them into a new datatype storing
the composite step function and starting state:
(,) <$> Fold.sum <*> Fold.length = Fold step (Pair 0 0) done
  where
    step (Pair x y) n = Pair (x + n) (y + 1)
    done (Pair x y) = (x, y)
… and then fold just transforms that to a strict left fold:
fold (Fold step begin done) = done (foldl' step begin)
Since we preserve the step function and accumulator, we can use the Fold type to
fold things other than pure collections.  For example, we can fold a Producer
from pipes using the same Fold:
Fold.purely Pipes.Prelude.fold ((,) <$> sum <*> length)
    :: (Monad m, Num a) => Producer a m () -> m (a, Int)
To learn more about this library, read the documentation in
the main Control.Foldl module.
Quick start
Install the stack tool and then run:
$ stack setup
$ stack ghci foldl
Prelude> import qualified Control.Foldl as Fold
Prelude Fold> Fold.fold ((,) <$> Fold.sum <*> Fold.length) [1..1000000]
(500000500000,1000000)
How to contribute
Contribute a pull request if you have a Fold that you believe other people
would find useful.
Development Status
The foldl library is pretty stable at this point.  I don’t expect there to be
breaking changes to the API from this point forward unless people discover new
bugs.
License (BSD 3-clause)
Copyright (c) 2016 Gabriella Gonzalez All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- 
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 - 
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 - 
Neither the name of Gabriella Gonzalez nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Changes
1.4.18
- Add 
lifts - Add 
nestforFold1 - Add 
Choice,Closed,Cosieve,Extend,Semigroupoid,Category,Strong,ArrowandArrowChoiceinstances forFold1 - Add 
Closedinstance forFold - Define 
stimesfromEndoM’s Semigroup instance for 0 
1.4.17
- Add Fold1 utilities: 
purely,purely_,premap,handles,foldOver,folded1 - Add pattern synonym 
Fold1_that makes the initial, step and extraction functions explicit. 
1.4.16
1.4.15
- Add 
CosieveandCostronginstances 
1.4.14
- Add 
Control.Foldl.NonEmpty.nonEmpty - Add 
Control.Foldl.NonEmpty.toFold - Generalize 
fold1to work withFoldable1 
1.4.13
- New “Control.Foldl.NonEmpty” module for folding non-empty containers
 
1.4.12
Data.Functor.Extend.Extendedinstances forFold/FoldM- Remove dependency on 
mwc-random 
1.4.11
- Fix doctest failure when built against newer versions of the 
hashablepackage 
1.4.10
- Fix space leaks in 
scan/scanM 
1.4.9
- Implement 
vectorutility more efficiently 
1.4.8
- Only depend on 
semigroupsfor older GHC versions 
1.4.7
- Add 
foldByKey{,Hash}Mapfunctions 
1.4.6
- Add 
nest/predropWhile/drop/dropM 
1.4.5
- Increase upper bound on 
containers - Add 
either/eitherM 
1.4.4
- Increase lower bound on 
base - Change 
meanto be more numerically stable 
1.4.3
- Add 
Control.Scanl.scanr - Increase upper bound on 
mwc-random 
1.4.2
- Add 
Semigroupoidinstance forFold - Increase upper bound on 
contravariantandprofunctors 
1.4.1
- Add 
Control.Scanl - Drop support for GHC 7.8 and older
 
1.4.0
- BREAKING CHANGE: Change type of 
premapMto accept a monadic function 
1.3.7
- Add 
groupBy 
1.3.6
- Documentation improvements
 
1.3.5
- Add 
Choiceinstance forFold 
1.3.4
- Add 
prefilterandprefilterM 
1.3.3
- Add back the old 
vectorasvectorM 
1.3.2
- Compatibility with 
Semigroupbecoming a super-class ofMonoid - Fix 
asinforFold 
1.3.1
- Fix 
asinforFoldM 
1.3.0
- BREAKING CHANGE: Change 
vectorto be a pureFold(which is faster, too!) 
1.2.5
- Add support for folding new containers: 
hashSet,map, andhashMap - Add 
prescan/postscanwhich generalizescantoTraversabletypes 
1.2.4
- Add 
lazyfolds forTextandByteString - Documentation fixes and improvements
 
1.2.3
- Add 
lookup 
1.2.2
- Add numerically stable 
mean,variance, andstdfolds - Add 
Control.Foldl.{Text,ByteString}.foldM - Add 
foldOver/foldOverM 
1.2.1
- Performance improvements
 - Re-export 
filtered 
1.2.0
- Breaking change: Fix 
handlesto fold things in the correct order (was previously folding things backwards and also leaking space as a result). No change to behavior ofhandlesM, which was folding things in the right order - Breaking change: Change the 
Monoidused byHandler/HandlerM - Add 
folded 
1.1.6
- Add 
maximumByandminimumBy 
1.1.5
- Increase lower bound on 
basefrom< 4to< 4.5 
1.1.4
- Increase upper bound on 
comonadfrom< 5to< 6 
1.1.3
- Increase upper bound on 
profunctorsfrom< 5.2to< 5.3 - Add 
mapM_,hoists,purely, andimpurely 
1.1.2
- Add 
lastN,randomN,sink, andduplicateM - Add 
Comonadinstance forFold - Add 
Profunctorinstance forFoldM 
1.1.1
- Increase upper bound on 
vectorfrom< 0.11to< 0.12 
1.1.0
- Breaking change: Rename 
pretraverse/pretraverseMtohandles/handlesM - Add 
Handler - Export 
EndoM 
1.0.11
- Add 
Profunctorinstance forFold 
1.0.10
- Add 
randomand_Fold1 
1.0.9
- Increase upper bound on 
primitivefrom< 0.6to< 0.7 
1.0.8
- Add 
revList 
1.0.7
- Add 
NumandFractionalinstances forFold/FoldM - Add 
countfold forTextandByteString 
1.0.6
- Add 
pretraverseandpretraverseM 
1.0.5
- Add 
lastDef 
1.0.4
- Increase upper bounds on 
transformersfrom< 0.4to< 0.6 - Add 
nub,eqNub, andset 
1.0.3
- Add 
scan,generalize,simplify, andpremapM 
1.0.2
- Add 
listandvectorfolds - Add 
foldfunction forTextandByteString 
1.0.1
- Add support for 
ByteStringandTextfolds - Add 
Monoidinstance forFold/FoldM 
1.0.0
- Initial release