I'm open to using friendlier keywords instead of monadic
and yield
. For example, after thinking for a while I came up with the keywords flat
instead of monadic
and map!
instead of yield
.
const foo = flat (x) => {
const y = map! [x * 1, x * 2, x * 3];
const z = map! [y + 1, y + 2, y + 3];
return z;
};
Note: Since map
is a commonly used identifier, I didn't want to make it a keyword. Hence, I put an exclamation mark after map
to make the keyword map!
. I like this because the exclamation mark denotes side effects, kind of like the command syntax proposed by @mlanza.
Anyway, these functions can be called flat functions, which is a lot friendlier than calling them monadic functions. In addition, monadic functions could imply "functions which have only one input", which could be confusing for beginners.
It also makes sense to use flat
and map!
because m.flatMap(f)
should be equivalent to m.map(f).flat()
. This should make it easier to explain to beginners and it should also make it easier to remember that map! m
expressions de-sugar to m.flatMap
method calls.
Flat functions allow you to use map! m
expressions, and then they flatten the results.
I don't buy that this syntax would be difficult for JavaScript programmers to learn for two reasons.
- JavaScript programmers are already familiar with the
async…await
syntax, which is the same thing asflat…map!
except that it's specialized for promises. Anawait p
expression can be de-sugared to ap.then
method call. Similarly, amap! m
expression can be de-sugared to anm.flatMap
method call. This shouldn't be too difficult for JavaScript programmers to grok. - JavaScript programmers don't need to understand the concept of a monad in order to use flat functions. They already know what the
map
method does, what theflat
method does, and what theflatMap
method does. We can explain the syntax using these three operations on arrays, without ever having to use the word "monad". And once they understand the syntax as it operates for arrays, then we can generalize and show that this syntax works for any data type whose values have aflatMap
method.