Haskell Hierarchical Libraries (collections package)ContentsIndex
Data.Collections.Foldable
PortabilityMPTC+FD
Stabilityexperimental
Maintainerjeanphilippe.bernardy (google mail address)
Contents
Folds
Special biased folds
Folding actions
Specialized folds
Searches
Description
Class of data structures that can be folded to a summary value.
Synopsis
class Foldable t a | t -> a where
fold :: Monoid a => t -> a
foldMap :: Monoid m => (a -> m) -> t -> m
foldr :: (a -> b -> b) -> b -> t -> b
foldl :: (b -> a -> b) -> b -> t -> b
foldr1 :: (a -> a -> a) -> t -> a
foldl1 :: (a -> a -> a) -> t -> a
null :: t -> Bool
size :: t -> Int
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 ()
mapM_ :: (Foldable t a, Monad m) => (a -> m b) -> t -> m ()
sequenceA_ :: forall f a t . (Foldable t (f a), Applicative f) => t -> f ()
sequence_ :: forall m a t . (Foldable t (m a), Monad m) => t -> m ()
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
Folds
class Foldable t a | t -> a where

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.

Methods
fold :: Monoid a => t -> a
Combine the elements of a structure using a monoid.
foldMap :: Monoid m => (a -> m) -> t -> m
Map each element of the structure to a monoid, and combine the results.
foldr :: (a -> b -> b) -> b -> t -> b

Right-associative fold of a structure.

foldr f z = foldr f z . toList
foldl :: (b -> a -> b) -> b -> t -> b

Left-associative fold of a structure.

foldl f z = foldl f z . toList
foldr1 :: (a -> a -> a) -> t -> a

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 -> a

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

foldl1 f = foldl1 f . toList
null :: t -> Bool
Tells whether the foldable is empty.
size :: t -> Int
Returns the size of the foldable.
show/hide Instances
Foldable IntSet Int
Foldable (IntMap a) (Int, a)
Foldable (Maybe a) a
Foldable (Seq a) a
Foldable (Set a) a
Foldable (Set a) a
Enum a => Foldable (Set a) a
Foldable (SetList [a]) a
Foldable [a] a
Ix i => Foldable (Array i a) (i, a)
Foldable (Map k a) (k, a)
Foldable (Map k a) (k, a)
Sequence c (k, v) => Foldable (AssocList c k v) (k, v)
Foldable m (k, v) => Foldable (ElemsView m k v) v
Foldable m (k, v) => Foldable (KeysView m k v) k
Sequence s k => Foldable (Trie s k v) (s, v)
Special biased folds
foldr' :: Foldable t a => (a -> b -> b) -> b -> t -> b
Fold over the elements of a structure, associating to the right, but strictly.
foldl' :: Foldable t b => (a -> b -> a) -> a -> t -> a
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 b
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 a
Monadic fold over the elements of a structure, associating to the left, i.e. from left to right.
Folding actions
traverse_ :: (Foldable t a, Applicative f) => (a -> f b) -> t -> f ()
Map each element of a structure to an action, evaluate these actions from left to right, and ignore the results.
mapM_ :: (Foldable t a, Monad m) => (a -> m b) -> t -> m ()
Map each element of a structure to an monadic action, evaluate these actions from left to right, and ignore the results.
sequenceA_ :: forall f a t . (Foldable t (f a), Applicative f) => t -> f ()
Evaluate each action in the structure from left to right, and ignore the results.
sequence_ :: forall m a t . (Foldable t (m a), Monad m) => t -> m ()
Evaluate each monadic action in the structure from left to right, and ignore the results.
Specialized folds
toList :: Foldable t a => t -> [a]
List of elements of a structure.
and :: Foldable t Bool => t -> Bool
and returns the conjunction of a container of Bools. For the result to be True, the container must be finite; False, however, results from a False value finitely far from the left end.
or :: Foldable t Bool => t -> Bool
or returns the disjunction of a container of Bools. For the result to be False, the container must be finite; True, however, results from a True value finitely far from the left end.
any :: Foldable t a => (a -> Bool) -> t -> Bool
Determines whether any element of the structure satisfies the predicate.
all :: Foldable t a => (a -> Bool) -> t -> Bool
Determines whether all elements of the structure satisfy the predicate.
sum :: (Foldable t a, Num a) => t -> a
The sum function computes the sum of the numbers of a structure.
product :: (Foldable t a, Num a) => t -> a
The product function computes the product of the numbers of a structure.
maximum :: (Foldable t a, Ord a) => t -> a
The largest element of the structure.
maximumBy :: Foldable t a => (a -> a -> Ordering) -> t -> a
minimum :: (Foldable t a, Ord a) => t -> a
The least element of the structure.
minimumBy :: Foldable t a => (a -> a -> Ordering) -> t -> a
Searches
elem :: (Foldable t a, Eq a) => a -> t -> Bool
Does the element occur in the structure?
notElem :: (Foldable t a, Eq a) => a -> t -> Bool
Produced by Haddock version 0.7