DEV Community

Functional JavaScript - Functors, Monads, and Promises

JavaScript Joel on November 06, 2018

Some people have said a Promise is a Monad. Others have said a Promise is not a Monad. They are both wrong... and they are both right. By the ti...
Collapse
 
jolmos profile image
jolmos

Thanks! Nice examples.
Just one suggestion: under title "Back to the Monad" you explain flatMap, but not Monad, and then you just say: "We have covered Functors and Monads".
It left me a bit confused until I read further.

I miss one sentence like "A Monad is similar to a Functor, but is Flat Mappable between Categories." in the "Back to the Monad" section.

Collapse
 
joelnet profile image
JavaScript Joel

Thanks for the feedback. I have added a couple of sentences a little closer to the flatMap example to hopefully add some clarity to this:

Maybe you were expecting more, but that's it for a Monad! A Monad is both mappable and flat-mappable.

Hope that helps!

Cheers!

Collapse
 
plepe profile image
Stephan Bösch-Plepelits

Found a mistake:
[1, 2, 3].map(x => x + 1) //=> [2, 4, 6]

Correct result:
[2, 3, 4]

Correct function for the result above:
[1, 2, 3].map(x => x * 2) //=> [2, 4, 6]

Collapse
 
joelnet profile image
JavaScript Joel

Great catch! I was so used to typing x => x * 2 that I didn't even notice I was using x => x + 1.

Article has been corrected.

Cheers!

Collapse
 
stealthmusic profile image
Jan Wedel

Great examples! The funny thing is, everything I read something about Monads, I think „now I understood it“. Two days later, I’ve can’t explain it anymore. Maybe I’ll have to explain it to someone first to remember it.

One question though: Isn’t map usually used to return a different type (at least in statically typed languages)? So I wonder why it returns itself again with a new value...

Collapse
 
joelnet profile image
JavaScript Joel

The type in question here is Array. So when you map on an array you still get an array.

For example, you might map a Number to a String, but you'd still be in an Array: Array<number> -> Array<string>

Collapse
 
raiondesu profile image
Alexey

Promises could've been purely monadic if not for some ignorant dudes...

github.com/promises-aplus/promises...

Collapse
 
lmbarr profile image
Luis Miguel

I love Feynman and your explanation!

Collapse
 
joelnet profile image
JavaScript Joel

Thanks!

Feynman has a video: Physics is fun to imagine or something like that. It's one of my favorite videos to rewatch over and over :)

Collapse
 
lmbarr profile image
Luis Miguel

Thanks for de recommendation I love that guy. The BBC documentary about Feynman work/life is good also.

Thread Thread
 
joelnet profile image
JavaScript Joel

For sure. I can't get enough. Even have a few of his books at home. He does a great job of explaining complex terms for the Lay person.

Collapse
 
yorodm profile image
Yoandy Rodriguez Martinez

You know, as long as Monad tutorials go, this is pretty cool and simple, would you mine if I write a translation in Spanish? Of course I will reference your work.

Collapse
 
joelnet profile image
JavaScript Joel

I am happy you found it useful :)

All translations welcome! Link it back here too, I am curious to see the translation :)

Cheers!