Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Foldable

Lean: Hale.Base.Foldable | Haskell: Data.Foldable

Overview

Typeclass for structures that can be folded to a summary value. Provides a rich API with many derived operations built on top of foldr.

API Mapping

LeanHaskellKind
Foldable classFoldableTypeclass
foldrfoldrMethod
foldlfoldl'Method
toListtoListMethod
foldMapfoldMapFunction
nullnullFunction
lengthlengthFunction
anyanyFunction
allallFunction
find?findFunction
elemelemFunction
minimum?minimumFunction
maximum?maximumFunction
sumsumFunction
productproductFunction

Instances

  • Foldable List
  • Foldable Option
  • Foldable List.NonEmpty
  • Foldable (Either a)

Proofs & Guarantees

None listed (correctness follows from typeclass laws).

Example

-- Sum all elements in a list
Foldable.sum [1, 2, 3]
-- => 6