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:

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:

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 Gabriel 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 Gabriel 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.3

Add Control.Scanl.scanr

Increase upper bound on mwc-random

1.4.2

Add Semigroupoid instance for Fold

Increase upper bound on contravariant and profunctors

1.4.1

Add Control.Scanl

Drop support for GHC 7.8 and older

1.4.0

BREAKING CHANGE: Change type of premapM to accept a monadic function

1.3.7

Add groupBy

1.3.6

Documentation improvements

1.3.5

Add Choice instance for Fold

1.3.4

Add prefilter and prefilterM

1.3.3

Add back the old vector as vectorM

1.3.2

Compatibility with Semigroup becoming a super-class of Monoid

Fix asin for Fold

1.3.1

Fix asin for FoldM

1.3.0

BREAKING CHANGE: Change vector to be a pure Fold (which is faster, too!)

1.2.5

Add support for folding new containers: hashSet, map, and hashMap

Add prescan/postscan which generalize scan to Traversable types

1.2.4

Add lazy folds for Text and ByteString

Documentation fixes and improvements

1.2.3

Add lookup

1.2.2

Add numerically stable mean, variance, and std folds

Add Control.Foldl.{Text,ByteString}.foldM

Add foldOver/foldOverM

1.2.1

Performance improvements

Re-export filtered

1.2.0

Breaking change: Fix handles to fold things in the correct order (was
previously folding things backwards and also leaking space as a result). No
change to behavior of handlesM, which was folding things in the right order