Foldable
A foldable is a data structure whose elements can be combined by a folding operation to produce a result.
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 →
Type-Driven Development with Idris
Foldable is an Idris interface for structures that can be reduced to a single value using an initial value and a combining function.
- How it is used
- Provides a uniform way to reduce parameterized structures to one value using an initial accumulator and combining function.
- How deep it goes
- Works through list folds and a recursive tree implementation, including foldr order; leaves foldl to its default definition.
- Examples
- Uses integer sums and products, total string length, and a tree folded from left to right.
- What you can do after
- Build from scratch a Foldable instance for a generic tree, or apply foldr to calculate aggregate values.
Read how Type-Driven Development with Idris 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.
What to know first
- listA list is an ordered collection of elements arranged in a sequence.
- stringA string is a programming-language value representing textual data as an ordered sequence of characters or encoded units.
Related concepts
- type classkind of · A type class defines a set of operations that types can support, enabling a common interface and ad hoc polymorphism.
- listimplemented by · A list is an ordered collection of elements arranged in a sequence.
- 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 applies an effectful function to each element of a structure and combines the resulting effects into one effect containing the transformed structure.
- right folduses · A right fold combines the elements of a structure with an operation, associating from the right and incorporating an initial value.
- type constructoruses · A type constructor is a construct that takes type parameters, and sometimes values, to form or instantiate a type.
Questions about foldable
What is foldable?
A foldable is a data structure whose elements can be combined by a folding operation to produce a result.
Which Manning books explain foldable?
Functional Programming in Scala, Second Edition, Type-Driven Development with Idris and Learn Haskell by Example.
What should I know before learning foldable?
Manning books that teach foldable usually introduce list and string first.
What is the difference between foldable and functor?
A foldable is a data structure whose elements can be combined by a folding operation to produce a result. A functor is an abstraction that maps values or objects while preserving the structure and relationships of their surrounding context.
What kinds of foldable are there?
Kinds of foldable include traverse.
What is foldable a kind of?
Foldable is a kind of type class. A type class defines a set of operations that types can support, enabling a common interface and ad hoc polymorphism.
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.


