Module

Control.Fold

This module provides a type Fold for lefts folds, which can be combined using the Applicative class:

average :: Fold Number Number
average = (/) <$> sum <*> length

Fold can be used to fold a Foldable structure (foldl), or scan a Traversable structure (scanl):

finalAverage = foldl average [1.0, 2.0, 3.0] :: Number
movingAverage = scanl average [1.0, 2.0, 3.0] :: Array Number

This library is based on the foldl library by Gabriel Gonzalez: http://hackage.haskell.org/package/foldl

#Fold

newtype Fold a b

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

Instances

#stepFold

stepFold :: forall a b. a -> Fold a b -> Fold a b

Step a fold by providing a single input.

#unfoldFold

unfoldFold :: forall s a b. s -> (s -> a -> 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_

unfoldFold_ :: forall a b. b -> (b -> a -> 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.

#foldl

foldl :: forall f a b. Foldable f => Fold a b -> f a -> b

Run a Fold by providing a Foldable container of inputs, and then generating a single output. This is analogous to the foldl function from Data.Foldable.

#scanl

scanl :: forall f a b. Traversable f => Fold a b -> f a -> f b

Run a Fold by providing a Traversable container of inputs, and generating an output for each input. This is analogous to the scanl function from Data.Traversable.

#mconcat

mconcat :: forall m. Monoid m => Fold m m

Fold values in some Monoid.

#head

head :: forall a. Fold a (Maybe a)

A Fold which remembers the first input.

#last

last :: forall a. Fold a (Maybe a)

A Fold which keeps the last input.

#null

null :: forall a. Fold a Boolean

A Fold which tests whether any inputs were seen.

#length

length :: forall a s. Semiring s => Fold a s

A Fold which counts its inputs.

#and

and :: forall b. HeytingAlgebra b => Fold b b

A Fold which tests if all of its inputs were true (generalized to work with an arbitrary HeytingAlgebra).

#or

or :: forall b. HeytingAlgebra b => Fold b b

A Fold which tests if any of its inputs were true (generalized to work with an arbitrary HeytingAlgebra).

#any

any :: forall a b. HeytingAlgebra b => (a -> b) -> Fold a b

A Fold which tests if any of its inputs satisfy some predicate (generalized to work with an arbitrary HeytingAlgebra).

#all

all :: forall a b. HeytingAlgebra b => (a -> b) -> Fold a b

A Fold which tests if all of its inputs satisfy some predicate (generalized to work with an arbitrary HeytingAlgebra).

#sum

sum :: forall s. Semiring s => Fold s s

A Fold which computes the sum of its inputs (generalized to work with an arbitrary Semiring).

#product

product :: forall s. Semiring s => Fold s s

A Fold which computes the product of its inputs (generalized to work with an arbitrary Semiring).

#maximum

maximum :: forall a. Bounded a => Fold a a

A Fold which computes the maximum of its inputs (generalized to work with an arbitrary Bounded type).

#minimum

minimum :: forall a. Bounded a => Fold a a

A Fold which computes the minimum of its inputs (generalized to work with an arbitrary Bounded type).

#elem

elem :: forall a. Eq a => a -> Fold a Boolean

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

#notElem

notElem :: forall a. Eq a => a -> Fold a Boolean

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

#distributed

distributed :: forall f a b. Distributive f => Fold a b -> Fold (f a) (f b)

Fold over entire collections of inputs, producing a collection of outputs.

#groupBy

groupBy :: forall a r g. Semigroup r => Ord g => (a -> g) -> Fold a r -> Fold a (SemigroupMap g r)

Perform a Fold while grouping the data according to a specified group projection function. Returns the folded result grouped as a map keyed by the group.

#prefilter

prefilter :: forall a b. (a -> Boolean) -> 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 a predicate pred.

Modules