pete-murphy/elm-folds/1.0.0

Fold

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 length

A 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 ] average

This library is based on the foldl library by Gabriella Gonzalez.

Type

type Fold a b

A left fold, which takes zero or more values of type a as input and produces output of type b.

Constructors

unfoldFold : s -> (a -> s -> s) -> (s -> b) -> Fold a b

Create a Fold by providing an initial state, a function which updates that state, and a function which produces output from a state.

unfoldFold_ : b -> (a -> b -> b) -> Fold a b

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.

succeed : b -> Fold a b

A Fold which ignores its input and always produces the given value.

This is useful as the starting point of an andMap pipeline.

Destructors

foldList : List a -> Fold a b -> b

Run a Fold on a list of inputs, and then generate a single output. This is analogous to List.foldl.

scanList : List a -> Fold a b -> List b

Run a Fold on a list of inputs, generating an output for each input.

stepFold : a -> Fold a b -> Fold a b

Step a fold by providing a single input.

Common folds

last : Fold a (Maybe a)

A Fold which keeps the last input.

null : Fold a Bool

A Fold which tests whether any inputs were seen.

length : Fold a number

A Fold which counts its inputs.

and : Fold Bool Bool

A Fold which tests if all of its inputs were true.

or : Fold Bool Bool

A Fold which tests if any of its inputs were true.

all : (a -> Bool) -> Fold a Bool

A Fold which tests if all of its inputs satisfy some predicate.

any : (a -> Bool) -> Fold a Bool

A Fold which tests if any of its inputs satisfy some predicate.

sum : Fold number number

A Fold which computes the sum of its inputs.

product : Fold number number

A Fold which computes the product of its inputs.

maximum : Fold comparable (Maybe comparable)

A Fold which computes the maximum of its inputs, or Nothing if there were no inputs.

minimum : Fold comparable (Maybe comparable)

A Fold which computes the minimum of its inputs, or Nothing if there were no inputs.

Statistical folds

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.

average : Fold Float Float

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 length
mean : Fold Float (Maybe Float)

A 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.

variance : Fold Float (Maybe Float)

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.

standardDeviation : Fold Float (Maybe Float)

A Fold which computes the population standard deviation of its inputs, or Nothing if there were no inputs.

sampleVariance : Fold Float (Maybe Float)

A Fold which computes the sample (Bessel-corrected, n - 1) variance of its inputs, or Nothing if fewer than two inputs were seen.

sampleStandardDeviation : Fold Float (Maybe Float)

A Fold which computes the sample standard deviation of its inputs, or Nothing if fewer than two inputs were seen.

Membership

elem : a -> Fold a Bool

A Fold which tests if a specific value appeared as an input.

notElem : a -> Fold a Bool

A Fold which tests if a specific value did not appear as an input.

Combinators

map : (b -> c) -> Fold a b -> Fold a c

Transform the output of a Fold.

map2 : (b -> c -> d) -> Fold a b -> Fold a c -> Fold a d

Combine the results of two Folds run in parallel over the same input.

average : Fold Float Float
average =
    map2 (/) sum length
map3 : (b -> c -> d -> e) -> Fold a b -> Fold a c -> Fold a d -> Fold a e

Combine the results of three Folds run in parallel over the same input.

(b -> c -> d -> e -> g)
-> Fold a b
-> Fold a c
-> Fold a d
-> Fold a e
-> Fold a g

Combine the results of four Folds run in parallel over the same input.

(b -> c -> d -> e -> g -> h)
-> Fold a b
-> Fold a c
-> Fold a d
-> Fold a e
-> Fold a g
-> Fold a h

Combine the results of five Folds run in parallel over the same input.

(b -> c -> d -> e -> g -> h -> i)
-> Fold a b
-> Fold a c
-> Fold a d
-> Fold a e
-> Fold a g
-> Fold a h
-> Fold a i

Combine the results of six Folds run in parallel over the same input.

andMap : Fold a x -> Fold a (x -> y) -> Fold a y

Chain a Fold producing a function onto a Fold producing its argument. Designed for pipeline style, when map2–map6 aren't enough:

succeed (\a b c -> ( a, b, c ))
    |> andMap foldA
    |> andMap foldB
    |> andMap foldC
extend : (Fold a b -> c) -> Fold a b -> Fold a c

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.

groupBy : (a -> comparable) -> Fold a r -> Fold a (Dict comparable r)

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 : (a -> Bool) -> Fold a b -> Fold a b

prefilter pred f returns a new Fold based on f but where inputs will only be included if they satisfy the predicate pred.

premap : (a -> b) -> Fold b r -> Fold a r

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) sum
distributed : Fold a b -> Fold (e -> a) (e -> b)

Lift 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