class Foldable f where
foldr :: forall a b. (a -> b -> b) -> b -> f a -> b
foldl :: forall a b. (b -> a -> b) -> b -> f a -> b
foldMap :: forall a m. (Monoid m) => (a -> m) -> f a -> mFoldable represents data structures which can be folded.
foldrfolds a structure from the rightfoldlfolds a structure from the leftfoldMapfolds a structure by accumulating values in aMonoid
Default implementations are provided by the following functions:
foldrDefaultfoldlDefaultfoldMapDefaultRfoldMapDefaultL
Note: some combinations of the default implementations are unsafe to use together - causing a non-terminating mutually recursive cycle. These combinations are documented per function.
instance foldableArray :: Foldable Array
instance foldableMaybe :: Foldable Maybe
instance foldableFirst :: Foldable First
instance foldableLast :: Foldable Last
instance foldableAdditive :: Foldable Additive
instance foldableDual :: Foldable Dual
instance foldableDisj :: Foldable Disj
instance foldableConj :: Foldable Conj
instance foldableMultiplicative :: Foldable MultiplicativefoldrDefault :: forall f a b. (Foldable f) => (a -> b -> b) -> b -> f a -> bA default implementation of foldr using foldMap.
Note: when defining a Foldable instance, this function is unsafe to use
in combination with foldMapDefaultR.
foldlDefault :: forall f a b. (Foldable f) => (b -> a -> b) -> b -> f a -> bA default implementation of foldl using foldMap.
Note: when defining a Foldable instance, this function is unsafe to use
in combination with foldMapDefaultL.
foldMapDefaultR :: forall f a m. (Foldable f, Monoid m) => (a -> m) -> f a -> mA default implementation of foldMap using foldr.
Note: when defining a Foldable instance, this function is unsafe to use
in combination with foldrDefault.
foldMapDefaultL :: forall f a m. (Foldable f, Monoid m) => (a -> m) -> f a -> mA default implementation of foldMap using foldl.
Note: when defining a Foldable instance, this function is unsafe to use
in combination with foldlDefault.
fold :: forall f m. (Foldable f, Monoid m) => f m -> mFold a data structure, accumulating values in some Monoid.
traverse_ :: forall a b f m. (Applicative m, Foldable f) => (a -> m b) -> f a -> m UnitTraverse a data structure, performing some effects encoded by an
Applicative functor at each value, ignoring the final result.
For example:
traverse_ print [1, 2, 3]for_ :: forall a b f m. (Applicative m, Foldable f) => f a -> (a -> m b) -> m UnitA version of traverse_ with its arguments flipped.
This can be useful when running an action written using do notation for every element in a data structure:
For example:
for_ [1, 2, 3] \n -> do
print n
trace "squared is"
print (n * n)sequence_ :: forall a f m. (Applicative m, Foldable f) => f (m a) -> m UnitPerform all of the effects in some data structure in the order
given by the Foldable instance, ignoring the final result.
For example:
sequence_ [ trace "Hello, ", trace " world!" ]mconcat :: forall f m. (Foldable f, Monoid m) => f m -> mFold a data structure, accumulating values in some Monoid.
intercalate :: forall f m. (Foldable f, Monoid m) => m -> f m -> mFold a data structure, accumulating values in some Monoid,
combining adjacent elements using the specified separator.
and :: forall a f. (Foldable f, BooleanAlgebra a) => f a -> aTest whether all Boolean values in a data structure are true.
or :: forall a f. (Foldable f, BooleanAlgebra a) => f a -> aTest whether any Boolean value in a data structure is true.
any :: forall a b f. (Foldable f, BooleanAlgebra b) => (a -> b) -> f a -> bTest whether a predicate holds for any element in a data structure.
all :: forall a b f. (Foldable f, BooleanAlgebra b) => (a -> b) -> f a -> bTest whether a predicate holds for all elements in a data structure.
sum :: forall a f. (Foldable f, Semiring a) => f a -> aFind the sum of the numeric values in a data structure.
product :: forall a f. (Foldable f, Semiring a) => f a -> aFind the product of the numeric values in a data structure.
elem :: forall a f. (Foldable f, Eq a) => a -> f a -> BooleanTest whether a value is an element of a data structure.
notElem :: forall a f. (Foldable f, Eq a) => a -> f a -> BooleanTest whether a value is not an element of a data structure.
find :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Maybe aTry to find an element in a data structure which satisfies a predicate.