functional-programmingfoldcatamorphism# What are practical examples of the higher-order functions foldl and foldr?

The typical academic example is to sum a list. Are there real world examples of the use of fold that will shed light on its utility ?

Solution

`fold`

is perhaps the most fundamental operation on sequences. Asking for its utility is like asking for the utility of a `for`

loop in an imperative language.

Given a list (or array, or tree, or ..), a starting value, and a function, the `fold`

operator reduces the list to a single result. It is also the natural catamorphism (destructor) for lists.

Any operations that take a list as input, and produce an output after inspecting the elements of the list can be encoded as folds. E.g.

```
sum = fold (+) 0
length = fold (λx n → 1 + n) 0
reverse = fold (λx xs → xs ++ [x]) []
map f = fold (λx ys → f x : ys) []
filter p = fold (λx xs → if p x then x : xs else xs) []
```

The fold operator is not speciﬁc to lists, but can be generalised in a uniform way to ‘regular’ datatypes.

So, as one of the most fundamental operations on a wide variety of data types, it certainly does have some use out there. Being able to recognize when an algorithm can be described as a fold is a useful skill that will lead to cleaner code.

References:

- Using apply for calculating subscale and total scores across multiple dataframes
- How to implement db capability approach with pure functions in F#?
- Find a pair of integers from an array that sums up to a given number in Java 8 using functionals
- How to join array of optional integers to string?
- Which functions in Array.prototype are pure function in Javascript
- Is this property of a functor stronger than a monad?
- How do I make a minimal working example for the a DBus server?
- unfamiliar syntax in Haskell / Clash function type signature
- How to debug with PureScript?
- Generic Map function in Golang
- Learning functional/Clojure programming - practical exercises?
- Python functional method of checking that all elements in a list are equal
- What is the proper way of wrapping an Int (not a general type) in another type if type safety is the only motive?
- javascript fold reduce functional programming
- Typescript types for a pipe() function
- Can you explain closures (as they relate to Python)?
- Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip)
- Unable to understand the strange "where" syntax in Haskell / Clash
- Codensity and ContT
- Python fluent filter, map, etc
- functional programming in python - using map(), filter(), and sum() together - java .stream() equivalent?
- Haskell foldr1 lambda function which adds tuple values
- How can I get the nested keys of a map in clojure?
- How do I use System.Console.ANSI to wrap a String in escape sequences for getting it colored in the terminal?
- Flux.switchIfEmpty - what if it doesn't switch when first Flux is completed?
- Does Raku has a data type for encoding side effects as pure values?
- Does Rust has a data type for encoding side effects as pure values?
- Getting the underlying type of slice passed as type argument to a type parameter in the receiver of a method
- Why is immutability so important (or needed) in JavaScript?
- fp-ts - how to reverse curried function order