Module

Control.Fold

Package
purescript-open-folds
Repository
purescript-open-community/purescript-open-folds

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 Source

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 Source

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

Step a fold by providing a single input.

#unfoldFold Source

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_ Source

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 Source

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 Source

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 Source

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

Fold values in some Monoid.

#head Source

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

A Fold which remembers the first input.

#last Source

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

A Fold which keeps the last input.

#null Source

null :: forall a. Fold a Boolean

A Fold which tests whether any inputs were seen.

#length Source

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

A Fold which counts its inputs.

#and Source

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 Source

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 Source

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 Source

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 Source

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 Source

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 Source

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 Source

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 Source

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

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

#notElem Source

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

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

#distributed Source

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 Source

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 Source

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
Control.Fold