Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the connection between generator functions and monads in JavaScript?

A software development enthusiast noted the following:

Current status: compulsively rewriting all my JS after making the intuitive connection between generator functions and monad comprehensions.

I felt like I missed something there. My solution to callback hell was to use something like js-csp (ie a queue).

My question is: What is the connection between generator functions and monads in JavaScript?

like image 411
hawkeye Avatar asked Aug 23 '15 09:08

hawkeye


1 Answers

There is none.

While generator functions may look very similar to monad comprehensions (i.e. do notation), they are not as generic. The main problem is that ES6 generators are stateful, and can only be advanced once per continuation. Try implementing the list monad with them and see it fail.

True monad comprehensions can be achieved using a compile-to-JS language that supports them (like LatteJs, monadic, PureScript or LispyScript), or sweet.js macros. They typically desugar to callbacks - just like in Haskell.

like image 126
Bergi Avatar answered Oct 16 '22 09:10

Bergi