Foldable
Foldable is a typeclass abstraction for data structures whose elements can be traversed and reduced to a result by folding operations.
Functional Programming in Scala, Second Edition
A foldable data type is a data type whose elements can be traversed and reduced to a result by a folding operation.
- How it is used
- Abstracts reductions over different container shapes through foldLeft, foldRight, foldMap, and combineAll.
- How deep it goes
- Derives fold operations, compares implementation choices, and implements instances for List, IndexedSeq, LazyList, Tree, and Option.
- Examples
- Uses integer summation, List, IndexedSeq, LazyList, Tree, Option, conversion to List, and frequency-map construction.
- What you can do after
- Build it from scratch by defining and optimizing Foldable instances and deriving reusable reductions.
Read how Functional Programming in Scala, Second Edition explains foldable →
Learn Haskell by Example
Foldable is a Haskell type class for data structures whose elements can be traversed and reduced by folding operations.
- How it is used
- Generalizes list reduction across traversable data structures and supplies convenient aggregate operations.
- How deep it goes
- Defines fold directions, gives recursive implementations, and stops short of showing the binary-tree instance code.
- Examples
- Uses candy trails, fruity-candy collection, last-five collection, binary trees, and expression-building folds.
- What you can do after
- Build it from scratch by implementing fold-based helpers and a Foldable instance for a binary tree.
Related concepts
- type classkind of · A type class is a language feature defining operations and constraints that types can satisfy to support ad hoc polymorphism.
- functorcontrasts with · A functor is an abstraction that maps values or objects while preserving the structure and relationships of their surrounding context.
- traverseincludes · Traverse is an operation that maps an effectful function across a traversable structure and collects the results within an applicative context.
- right folduses · A right fold combines the elements of a structure from right to left with an operation and an initial value.
- type constructoruses · A type constructor is a type-level function that takes types as parameters and produces a parameterized type.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

