Foldable captures the pattern of folding a structure into a single value.
For a Foldable container $F$:
$$\text{foldr}(f, z, [x_1, \ldots, x_n]) = f(x_1, f(x_2, \ldots f(x_n, z)))$$
$$\text{foldl}(f, z, [x_1, \ldots, x_n]) = f(\ldots f(f(z, x_1), x_2) \ldots, x_n)$$
- foldr {α β : Type u} : (α → β → β) → β → F α → β
Right fold: $\text{foldr}(f, z, t) = f(x_1, f(x_2, \ldots f(x_n, z)))$.
- foldl {β α : Type u} : (β → α → β) → β → F α → β
Left fold: $\text{foldl}(f, z, t) = f(\ldots f(f(z, x_1), x_2) \ldots, x_n)$.
Convert to a list, preserving order.
Instances
Map each element to a monoid and combine. $$\text{foldMap}(f, [x_1, \ldots, x_n]) = f(x_1) \mathbin{++} f(x_2) \mathbin{++} \cdots \mathbin{++} f(x_n)$$
Uses Append and starts from mempty.
Equations
- Data.Foldable.foldMap f t = Data.Foldable.foldr (fun (a : α) (b : β) => f a ++ b) default t
Instances For
Is the structure empty? $$\text{null}(t) \iff |t| = 0$$
Equations
- Data.Foldable.null t = Data.Foldable.foldr (fun (x : α) (x_1 : Bool) => false) true t
Instances For
Count of elements. $$\text{length}(t) = |t|$$
Equations
- Data.Foldable.length t = Data.Foldable.foldl (fun (n : Nat) (x : α) => n + 1) 0 t
Instances For
Does any element satisfy the predicate? $$\text{any}(p, t) \iff \exists x \in t,\; p(x) = \text{true}$$
Equations
- Data.Foldable.any p t = Data.Foldable.foldr (fun (a : α) (b : Bool) => p a || b) false t
Instances For
Do all elements satisfy the predicate? $$\text{all}(p, t) \iff \forall x \in t,\; p(x) = \text{true}$$
Equations
- Data.Foldable.all p t = Data.Foldable.foldr (fun (a : α) (b : Bool) => p a && b) true t
Instances For
Is the element in the structure? $$\text{elem}(x, t) \iff \exists y \in t,\; x = y$$
Equations
- Data.Foldable.elem a t = Data.Foldable.any (fun (x : α) => x == a) t
Instances For
The minimum element, if the structure is non-empty.
Equations
- Data.Foldable.minimum? t = Data.Foldable.foldl (fun (acc : Option α) (a : α) => some (match acc with | none => a | some m => min m a)) none t
Instances For
The maximum element, if the structure is non-empty.
Equations
- Data.Foldable.maximum? t = Data.Foldable.foldl (fun (acc : Option α) (a : α) => some (match acc with | none => a | some m => max m a)) none t
Instances For
Sum of all elements. $$\text{sum}(t) = \sum_{x \in t} x$$
Equations
- Data.Foldable.sum t = Data.Foldable.foldl (fun (x1 x2 : α) => x1 + x2) 0 t
Instances For
Product of all elements. $$\text{product}(t) = \prod_{x \in t} x$$
Equations
- Data.Foldable.product t = Data.Foldable.foldl (fun (x1 x2 : α) => x1 * x2) 1 t
Instances For
Total minimum on a non-empty structure. No Option needed.
$$\text{minimum1}([x_1, \ldots, x_n]) = \min(x_1, \ldots, x_n)$$
Equations
- Data.Foldable.minimum1 ne = List.foldl (fun (acc a : α) => if (compare a acc == Ordering.lt) = true then a else acc) ne.head ne.tail
Instances For
Total maximum on a non-empty structure. No Option needed.
$$\text{maximum1}([x_1, \ldots, x_n]) = \max(x_1, \ldots, x_n)$$
Equations
- Data.Foldable.maximum1 ne = List.foldl (fun (acc a : α) => if (compare a acc == Ordering.gt) = true then a else acc) ne.head ne.tail
Instances For
Equations
- Data.instFoldableList = { foldr := fun {α β : Type ?u.10} => List.foldr, foldl := fun {β α : Type ?u.10} => List.foldl, toList := fun {α : Type ?u.10} => id }
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
foldr on an empty list is the initial accumulator.
foldl on an empty list is the initial accumulator.