Data structures that can be folded.
Data structures that can be folded.
Minimal complete definition: Foldable.foldMap or Foldable.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 where 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. Alternatively, one could define foldr:
instance Foldable Tree where foldr f z Empty = z foldr f z (Leaf x) = f x z foldr f z (Node l k r) = foldr f (f k (foldr f z r)) l
Combine the elements of a structure using a monoid.
versions without base case
A variant of fold that has no base case, and thus may only be applied to non-empty structures.
(not in Haskell's Foldable, because they have no Semigroup)
Map each element of the structure to a monoid, and combine the results.
A variant of foldMap that has no base case, and thus may only be applied to non-empty structures.
(not in Haskell's Foldable, because they have no Semigroup)
Left-associative fold of a structure.
nowarn: not easy enough
A variant of foldl that has no base case, and thus may only be applied to non-empty structures.
Right-associative fold of a structure.
nowarn: not easy enough
A variant of foldr that has no base case, and thus may only be applied to non-empty structures.
Fold over the elements of a structure, associating to the right, but strictly.
Monadic fold over the elements of a structure, associating to the right, i.e. from right to left.
Fold over the elements of a structure, associating to the left, but strictly.
Monadic fold over the elements of a structure, associating to the left, i.e. from left to right.
Map each element of a structure to an action, evaluate these actions from left to right, and ignore the results.
Map each element of a structure to a monadic action, evaluate these actions from left to right, and ignore the results.
Evaluate each action in the structure from left to right, and ignore the results.
Evaluate each monadic action in the structure from left to right, and ignore the results.
The sum of a collection of actions, generalizing concat.
The concatenation of all the elements of a container of lists.
Map a function over all the elements of a container and concatenate the resulting lists.
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 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.
Determines whether any element of the structure satisfies the predicate.
Determines whether all elements of the structure satisfy the predicate.
The sum function computes the sum of the numbers of a structure.
The product function computes the product of the numbers of a structure.
The largest element of a non-empty structure.
The largest element of a non-empty structure with respect to the given comparison function.
The least element of a non-empty structure.
The least element of a non-empty structure with respect to the given comparison function.
Does the element occur in the structure?
The find function takes a predicate and a structure and returns the leftmost element of the structure matching the predicate, or Maybe.Nothing if there is no such element.
inherited from Foldable.fold
inherited from Foldable.fold1
inherited from Foldable.foldMap1
inherited from Foldable.foldl
inherited from Foldable.foldl1
inherited from Foldable.foldr
inherited from Foldable.foldr1
inherited from Foldable.fold
inherited from Foldable.fold1
inherited from Foldable.foldMap
inherited from Foldable.foldMap1
inherited from Foldable.foldl1
inherited from Foldable.foldr1
inherited from Foldable.fold
inherited from Foldable.fold1
inherited from Foldable.foldMap
inherited from Foldable.foldMap1
inherited from Foldable.foldl1
inherited from Foldable.foldr1