This module provides a type Fold for left folds, which can be
combined using map2 (or, more generally, succeed and
andMap in pipeline style):
average : Fold Float Float
average =
map2 (/) sum lengthA Fold can be used to fold a list (foldList) or to scan a list
(scanList):
finalAverage : Float
finalAverage =
foldList [ 1, 2, 3 ] average
movingAverage : List Float
movingAverage =
scanList [ 1, 2, 3 ] averageThis library is based on the foldl library by Gabriella Gonzalez.
Create a Fold by providing an initial state, a function which updates
that state, and a function which produces output from a state.
Create a Fold by providing an initial state and a function which updates
that state. This is a variant of unfoldFold where the output
is the state itself.
A Fold which ignores its input and always produces the given value.
This is useful as the starting point of an andMap pipeline.
Run a Fold on a list of inputs, and then generate a single output. This
is analogous to List.foldl.
Run a Fold on a list of inputs, generating an output for each input.
A Fold which computes the maximum of its inputs, or Nothing if there
were no inputs.
A Fold which computes the minimum of its inputs, or Nothing if there
were no inputs.
Numerically stable single-pass statistics, computed via
Welford's online algorithm. All return Maybe to keep the empty
(and, for sample-based folds, singleton) cases honest.
A Fold which computes the arithmetic mean as sum / length. Returns
NaN for the empty list.
Compared to mean, this commits to a single final division and so
is slightly more accurate on well-scaled inputs — but sum can overflow to
Infinity when many large values are combined. Prefer mean when
you also need a variance (the state is shared via map2), or
when overflow is a concern, or when you want Maybe semantics on empty
input.
average : Fold Float Float
average =
map2 (/) sum lengthA Fold which computes the arithmetic mean of its inputs via Welford's
online algorithm, or Nothing if there were no inputs.
The running mean stays bounded by the data's range (so it never overflows)
and shares state with variance and friends when combined via
map2. For a one-shot mean of well-scaled data, average is
simpler and slightly more accurate.
A Fold which computes the population variance of its inputs, or
Nothing if there were no inputs. For a singleton input the variance is 0.
A Fold which computes the population standard deviation of its inputs,
or Nothing if there were no inputs.
A Fold which computes the sample (Bessel-corrected, n - 1) variance of
its inputs, or Nothing if fewer than two inputs were seen.
A Fold which computes the sample standard deviation of its inputs, or
Nothing if fewer than two inputs were seen.
Combine the results of two Folds run in parallel over the same input.
average : Fold Float Float
average =
map2 (/) sum lengthCombine the results of three Folds run in parallel over the same input.
Combine the results of four Folds run in parallel over the same input.
Combine the results of five Folds run in parallel over the same input.
Combine the results of six Folds run in parallel over the same input.
The comonadic extend operation. Given a function that turns a Fold into
an output, produce a Fold that emits that output at every step, taking into
account all input seen so far.
Perform a Fold while grouping the data according to a specified group
projection function. Returns the folded result grouped as a Dict keyed by
the group.
prefilter pred f returns a new Fold based on f but where inputs will
only be included if they satisfy the predicate pred.
premap f fold returns a new Fold which applies f to each input
before handing it to fold. This is the contravariant map on the input side
(compare with map, which transforms the output).
sumOfSquares : Fold Float Float
sumOfSquares =
premap (\x -> x * x) sumLift a Fold so it consumes a stream of functions e -> a and produces a
function e -> b. At every point e, the resulting function is the value the
original fold would have produced from the stream of as obtained by applying
each input function to e.
foldList [ \e -> e + 1, \e -> e * 2, \e -> e - 3 ] (distributed sum) 10
--> 38 -- == 11 + 20 + 7
A left fold, which takes zero or more values of type
aas input and produces output of typeb.