Portability | MPTC+FD |
---|---|

Stability | experimental |

Maintainer | jeanphilippe.bernardy (google mail address) |

Class of data structures that can be folded to a summary value.

- class Foldable t a | t -> a where
- foldr' :: Foldable t a => (a -> b -> b) -> b -> t -> b
- foldl' :: Foldable t b => (a -> b -> a) -> a -> t -> a
- foldrM :: (Foldable t a, Monad m) => (a -> b -> m b) -> b -> t -> m b
- foldlM :: (Foldable t b, Monad m) => (a -> b -> m a) -> a -> t -> m a
- traverse_ :: (Foldable t a, Applicative f) => (a -> f b) -> t -> f ()
- for_ :: (Foldable t a, Applicative f) => t -> (a -> f b) -> f ()
- sequenceA_ :: forall f a t. (Foldable t (f a), Applicative f) => t -> f ()
- asum :: (Foldable t (f a), Alternative f) => t -> f a
- mapM_ :: (Foldable t a, Monad m) => (a -> m b) -> t -> m ()
- forM_ :: (Foldable t a, Monad m) => t -> (a -> m b) -> m ()
- sequence_ :: forall m a t. (Foldable t (m a), Monad m) => t -> m ()
- msum :: (Foldable t (m a), MonadPlus m) => t -> m a
- toList :: Foldable t a => t -> [a]
- and :: Foldable t Bool => t -> Bool
- or :: Foldable t Bool => t -> Bool
- any :: Foldable t a => (a -> Bool) -> t -> Bool
- all :: Foldable t a => (a -> Bool) -> t -> Bool
- sum :: (Foldable t a, Num a) => t -> a
- product :: (Foldable t a, Num a) => t -> a
- maximum :: (Foldable t a, Ord a) => t -> a
- maximumBy :: Foldable t a => (a -> a -> Ordering) -> t -> a
- minimum :: (Foldable t a, Ord a) => t -> a
- minimumBy :: Foldable t a => (a -> a -> Ordering) -> t -> a
- elem :: (Foldable t a, Eq a) => a -> t -> Bool
- notElem :: (Foldable t a, Eq a) => a -> t -> Bool
- find :: Foldable t a => (a -> Bool) -> t -> Maybe a

# Folds

class Foldable t a | t -> a whereSource

Data structures that can be folded.

Minimal complete definition: `foldMap`

or `foldr`

.

For example, given a data type

data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)

a suitable instance would be

instance Foldable Tree foldMap f Empty = mempty foldMap f (Leaf x) = f x foldMap f (Node l k r) = foldMap f l `mappend` f k `mappend` foldMap f r

This is suitable even for abstract types, as the monoid is assumed to satisfy the monoid laws.

fold :: Monoid a => t -> aSource

Combine the elements of a structure using a monoid.

foldMap :: Monoid m => (a -> m) -> t -> mSource

Map each element of the structure to a monoid, and combine the results.

foldr :: (a -> b -> b) -> b -> t -> bSource

foldl :: (b -> a -> b) -> b -> t -> bSource

foldr1 :: (a -> a -> a) -> t -> aSource

A variant of `foldr`

that has no base case,
and thus may only be applied to non-empty structures.

`foldr1`

f =`foldr1`

f .`toList`

foldl1 :: (a -> a -> a) -> t -> aSource

A variant of `foldl`

that has no base case,
and thus may only be applied to non-empty structures.

`foldl1`

f =`foldl1`

f .`toList`

Tells whether the structure is empty.

Returns the size of the structure.

isSingleton :: t -> BoolSource

Tells whether the structure contains a single element.

## Special biased folds

foldr' :: Foldable t a => (a -> b -> b) -> b -> t -> bSource

Fold over the elements of a structure, associating to the right, but strictly.

foldl' :: Foldable t b => (a -> b -> a) -> a -> t -> aSource

Fold over the elements of a structure, associating to the left, but strictly.

foldrM :: (Foldable t a, Monad m) => (a -> b -> m b) -> b -> t -> m bSource

Monadic fold over the elements of a structure, associating to the right, i.e. from right to left.

foldlM :: (Foldable t b, Monad m) => (a -> b -> m a) -> a -> t -> m aSource

Monadic fold over the elements of a structure, associating to the left, i.e. from left to right.

## Folding actions

### Applicative actions

traverse_ :: (Foldable t a, Applicative f) => (a -> f b) -> t -> f ()Source

Map each element of a structure to an action, evaluate these actions from left to right, and ignore the results.

for_ :: (Foldable t a, Applicative f) => t -> (a -> f b) -> f ()Source

sequenceA_ :: forall f a t. (Foldable t (f a), Applicative f) => t -> f ()Source

Evaluate each action in the structure from left to right, and ignore the results.

asum :: (Foldable t (f a), Alternative f) => t -> f aSource

The sum of a collection of actions, generalizing `concat`

.

### Monadic actions

mapM_ :: (Foldable t a, Monad m) => (a -> m b) -> t -> m ()Source

Map each element of a structure to a monadic action, evaluate these actions from left to right, and ignore the results.

sequence_ :: forall m a t. (Foldable t (m a), Monad m) => t -> m ()Source

Evaluate each monadic action in the structure from left to right, and ignore the results.

msum :: (Foldable t (m a), MonadPlus m) => t -> m aSource

The sum of a collection of actions, generalizing `concat`

.

## Specialized folds

any :: Foldable t a => (a -> Bool) -> t -> BoolSource

Determines whether any element of the structure satisfies the predicate.

all :: Foldable t a => (a -> Bool) -> t -> BoolSource

Determines whether all elements of the structure satisfy the predicate.

sum :: (Foldable t a, Num a) => t -> aSource

The `sum`

function computes the sum of the numbers of a structure.

product :: (Foldable t a, Num a) => t -> aSource

The `product`

function computes the product of the numbers of a structure.

maximumBy :: Foldable t a => (a -> a -> Ordering) -> t -> aSource

The largest element of a non-empty structure with respect to the given comparison function.

minimumBy :: Foldable t a => (a -> a -> Ordering) -> t -> aSource

The least element of a non-empty structure with respect to the given comparison function.