“A monad is a monoid in the category of endofunctors, what’s the problem?”
Why Learn Functional Programming in JavaScript? (Composing Software)
Eric Elliott

I’ve been coding just for a couple of years. And I’m not a mathematician. Anyway, let me share my humble understanding of some of the algebra weird words in functional-style JS, using normal words and no fancy math terms.
1. Functor: it’s just an object with a map method on it. And the map method is more about running a function in a particular context than iterating over stuff:

const myFunctor = val => ({ 
map: func => myFunctor(func(val)), // we need to put the result of func(val) back into our functor for chainability
    fold: func => func(val) // once we're done with chaining we just get the final value
// example
const randomCalc = myFunctor(3)
.map(num => num + 1) // result here: myFunctor(4)
.map(anotherNum => anotherNum - 2) // myFunctor(2)
.map(yetAnotherNum => yetAnotherNum * 100) // myFunctor(200)
.fold(num => num + "") // once we're tired of running stupid random things in chain, we just return the final result and take it out of our functor box.
console.log(randomCalc) // "200"

2. Monad is just a functor object with `of` and `chain` methods on it.

const myMonadBox = x => ({
map: f => myMonadBox(f(x)), // explained above
fold: f => f(x), // explained above
chain: f => f(x)

As you can see the chain and fold methods have the same syntax here. But they have a different meaning. The chain method (also known as flatMap) is needed to avoid putting a Box in a Box in a Box … when we do nesting. Because at the end of the day after all the nesting is done all we want is to get a single Box(result), but not Box(Box(Box(result))).

// A pseudo-code example
const jakePosts = ajaxGet('/user/jake')

We’re assuming here that our function ajaxGet is a wrapper around our monad, i.e. it can use the myMonadBox methods.

const jakePosts = 
.chain(userJake =>
.chain(jakePosts => `Jake's posts: ${jakePosts}`)))

Finally the of method. It’s just a quick way to put your value into your functor box:

const Box = x => ({
map: f => Box(f(x)),
flatMap: f => f(x),
fold: f => f(x)
Box.of = x => Box(x);

That would be my simple understanding of Monads.

I’ve just started learning functional programming. And it’s already clear to me that functional JS is much better than classical inheritance imitation in JS (constructor functions and dangerous keywords like new , class , extends etc).

P.S. I’ve read a few posts and watched a few tech talks on Functors, Monads and some other functional programming smart words. But the biggest understanding of it came to me through watching Brian Lonsdorf’s “Professor Frisby Introduces Composable Functional JavaScript”. It has some great explanation of many functional concepts. But the character’s voice of Professor Frisby made me really suffer when I was watching it. However, I would really recommend it to anyone, who wants to learn functional programming in javascript. And it’s free. It would be nice complementary material to Eric’s great posts on the subject. Here’s the link:


Like what you read? Give Alexander Sobolev a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.