The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Unbox types with foldMap

Unbox types with foldMap

2:50
We get a clearer definition of what it means to fold a type, then we look at the foldMap function
Watch this lesson now
Avatar
egghead.io

We get a clearer definition of what it means to fold a type, then we look at the foldMap function

Avatar
Przemysław

Actually it wasn't clear on the very beginning why this code works:

const res2 = Map({brian: Sum(10), sara: Sum(2)})
.fold(Sum.empty())

but when you look into immutable-ext code

fold : function(empty) {
return this.foldMap(x => x, empty)
},
foldMap : function(f, empty) {
return empty != null
? this.reduce((acc, x) => acc.concat(f(x)), empty)
: this.reduce((acc, x) => acc.concat(f(x)))
},

and then into immutable js docs

reduce(
reducer: (reduction: R, value: V, key: K, iter: Iterable) => R,
initialReduction?: R,
context?: any
): R

Everything become more clearer ;)

In reply to egghead.io
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?