Safe Haskell | None |
---|---|
Language | Haskell2010 |
- class Foldable t where
- fold :: Foldable t => forall m. Monoid m => t m -> m
- foldMap :: Foldable t => forall m a. Monoid m => (a -> m) -> t a -> m
- foldr :: Foldable t => forall a b. (a -> b -> b) -> b -> t a -> b
- foldr' :: Foldable t => forall a b. (a -> b -> b) -> b -> t a -> b
- foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b
- foldlLazy :: Foldable t => (b -> a -> b) -> b -> t a -> b
- foldl1 :: Foldable t => (a -> a -> a) -> t a -> Maybe a
- foldr1 :: Foldable t => (a -> a -> a) -> t a -> Maybe a
- toList :: Foldable t => forall a. t a -> [a]
- null :: Foldable t => forall a. t a -> Bool
- length :: Foldable t => forall a. t a -> Int
- elem :: Foldable t => forall a. Eq a => a -> t a -> Bool
- minimum :: (Ord a, Foldable t) => t a -> Maybe a
- maximum :: (Ord a, Foldable t) => t a -> Maybe a
- sum :: (Semiring a, Foldable t) => t a -> a
- product :: (Semiring a, Foldable t) => t a -> a
- head :: Foldable t => t a -> Maybe a
- last :: Foldable t => t a -> Maybe a
- (!!) :: Foldable f => f a -> Int -> Maybe a
- concat :: Foldable t => t [a] -> [a]
- concatMap :: Foldable t => (a -> [b]) -> t a -> [b]
- and :: Foldable t => t Bool -> Bool
- or :: Foldable t => t Bool -> Bool
- any :: Foldable t => (a -> Bool) -> t a -> Bool
- all :: Foldable t => (a -> Bool) -> t a -> Bool
- maximumBy :: Foldable t => (a -> a -> Ordering) -> t a -> Maybe a
- minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> Maybe a
- foldr2 :: (Foldable f, Foldable g) => (a -> b -> c -> c) -> c -> f a -> g b -> c
- foldr3 :: (Foldable f, Foldable g, Foldable h) => (a -> b -> c -> d -> d) -> d -> f a -> g b -> h c -> d
- notElem :: (Foldable t, Eq a) => a -> t a -> Bool
- find :: Foldable t => (a -> Bool) -> t a -> Maybe a
Folds
Data structures that can be folded.
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
Foldable
instances are expected to satisfy the following laws:
foldr f z t = appEndo (foldMap (Endo . f) t ) z
foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z
fold = foldMap id
sum
, product
, maximum
, and minimum
should all be essentially
equivalent to foldMap
forms, such as
sum = getSum . foldMap Sum
but may be less defined.
If the type is also a Functor
instance, it should satisfy
foldMap f = fold . fmap f
which implies that
foldMap f . fmap g = foldMap (f . g)
fold :: Monoid m => t m -> m #
Combine the elements of a structure using a monoid.
foldMap :: Monoid m => (a -> m) -> t a -> m #
Map each element of the structure to a monoid, and combine the results.
foldr :: (a -> b -> b) -> b -> t a -> b #
Right-associative fold of a structure.
In the case of lists, foldr
, when applied to a binary operator, a
starting value (typically the right-identity of the operator), and a
list, reduces the list using the binary operator, from right to left:
foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
Note that, since the head of the resulting expression is produced by
an application of the operator to the first element of the list,
foldr
can produce a terminating expression from an infinite list.
For a general Foldable
structure this should be semantically identical
to,
foldr f z =foldr
f z .toList
foldr' :: (a -> b -> b) -> b -> t a -> b #
Right-associative fold of a structure, but with strict application of the operator.
List of elements of a structure, from left to right.
Test whether the structure is empty. The default implementation is optimized for structures that are similar to cons-lists, because there is no general way to do better.
Returns the size/length of a finite structure as an Int
. The
default implementation is optimized for structures that are similar to
cons-lists, because there is no general way to do better.
elem :: Eq a => a -> t a -> Bool infix 4 #
Does the element occur in the structure?
fold :: Foldable t => forall m. Monoid m => t m -> m #
Combine the elements of a structure using a monoid.
foldMap :: Foldable t => forall m a. Monoid m => (a -> m) -> t a -> m #
Map each element of the structure to a monoid, and combine the results.
foldr :: Foldable t => forall a b. (a -> b -> b) -> b -> t a -> b #
Right-associative fold of a structure.
In the case of lists, foldr
, when applied to a binary operator, a
starting value (typically the right-identity of the operator), and a
list, reduces the list using the binary operator, from right to left:
foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
Note that, since the head of the resulting expression is produced by
an application of the operator to the first element of the list,
foldr
can produce a terminating expression from an infinite list.
For a general Foldable
structure this should be semantically identical
to,
foldr f z =foldr
f z .toList
foldr' :: Foldable t => forall a b. (a -> b -> b) -> b -> t a -> b #
Right-associative fold of a structure, but with strict application of the operator.
foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b Source #
Left-associative fold of a structure.
In the case of lists, foldl
, when applied to a binary
operator, a starting value (typically the left-identity of the operator),
and a list, reduces the list using the binary operator, from left to
right:
foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn
Note that to produce the outermost application of the operator the
entire input list must be traversed. This means that foldl
will
diverge if given an infinite list.
This version is strict, in contrast to Prelude's foldl
.
This ensures that each step of the fold is forced to weak head normal
form before being applied, avoiding the collection of thunks that would
otherwise occur. This is often what you want to strictly reduce a finite
list to a single, monolithic result (e.g. length
).
For a lazy version, use foldlLazy
.
For a general Foldable
structure this should be semantically identical
to,
foldl f z =foldl
f z .toList
foldlLazy :: Foldable t => (b -> a -> b) -> b -> t a -> b Source #
Left-associative fold of a structure but with lazy application of the operator.
Note that if you want an efficient left-fold, you probably want to
use foldl
instead of foldlLazy
. The reason for this is that latter does
not force the "inner" results (e.g. z
in the above example)
before applying them to the operator (e.g. to f
x1(
). This results
in a thunk chain f
x2)O(n)
elements long, which then must be evaluated from
the outside-in.
For a general Foldable
structure this should be semantically identical
to,
foldlLazy f z =foldl
f z .toList
null :: Foldable t => forall a. t a -> Bool #
Test whether the structure is empty. The default implementation is optimized for structures that are similar to cons-lists, because there is no general way to do better.
length :: Foldable t => forall a. t a -> Int #
Returns the size/length of a finite structure as an Int
. The
default implementation is optimized for structures that are similar to
cons-lists, because there is no general way to do better.
minimum :: (Ord a, Foldable t) => t a -> Maybe a Source #
The least element of a structure. Returns Nothing
on empty
structures.
>>>
minimum [1,2,3]
Just 1>>>
minimum []
Nothing
maximum :: (Ord a, Foldable t) => t a -> Maybe a Source #
The largest element of a structure. Returns Nothing
on empty
structures.
>>>
maximum [1,2,3]
Just 3>>>
maximum []
Nothing
sum :: (Semiring a, Foldable t) => t a -> a Source #
The sum
function computes the sum of the numbers of a structure.
sum (xs :: [Integer]) === foldl (+) 0 xs
product :: (Semiring a, Foldable t) => t a -> a Source #
The product
function computes the product of the numbers of a
structure.
product (xs :: [Integer]) === foldl (*) 1 xs
head :: Foldable t => t a -> Maybe a Source #
The first element of a structure, or Nothing
if it's empty.
>>>
head [1,2,3]
Just 1>>>
head []
Nothing
head xs === last (reverse xs)
last :: Foldable t => t a -> Maybe a Source #
The last element of a structure, or Nothing
if it's empty.
>>>
last [1,2,3]
Just 3>>>
last []
Nothing
last xs === head (reverse xs)
(!!) :: Foldable f => f a -> Int -> Maybe a infixl 9 Source #
Index (subscript) operator, starting from 0. Returns Nothing
for
out-of-range indices.
>>>
[1,2,3] !! 0
Just 1>>>
[1,2,3] !! (-1)
Nothing>>>
[1,2,3] !! 3
Nothing
Specialized folds
concat :: Foldable t => t [a] -> [a] #
The concatenation of all the elements of a container of lists.
concatMap :: Foldable t => (a -> [b]) -> t a -> [b] #
Map a function over all the elements of a container and concatenate the resulting lists.
any :: Foldable t => (a -> Bool) -> t a -> Bool #
Determines whether any element of the structure satisfies the predicate.
all :: Foldable t => (a -> Bool) -> t a -> Bool #
Determines whether all elements of the structure satisfy the predicate.
maximumBy :: Foldable t => (a -> a -> Ordering) -> t a -> Maybe a Source #
The largest element of a structure with respect to the given comparison
function. Returns Nothing
on an empty input.
maximum (xs :: [Int]) === maximumBy compare xs
minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> Maybe a Source #
The least element of a structure with respect to the given comparison
function. Returns Nothing
on an empty input.
minimum (xs :: [Int]) === minimumBy compare xs
foldr2 :: (Foldable f, Foldable g) => (a -> b -> c -> c) -> c -> f a -> g b -> c Source #
Fold over two Foldable
s at once.
zip xs ys === foldr2 (\x y zs -> (x,y) : zs) [] xs ys
foldr3 :: (Foldable f, Foldable g, Foldable h) => (a -> b -> c -> d -> d) -> d -> f a -> g b -> h c -> d Source #
Fold over three Foldable
s at once.
zip3 ws xs ys === foldr3 (\w x y zs -> (w,x,y) : zs) [] ws xs ys