# Monadic Functions

**URL:** <https://es.discourse.group/t/monadic-functions/1728>\
**Category:** 💡 Ideas\
**Created:** [June 5, 2023, 12:40pm UTC](https://es.discourse.group/t/monadic-functions/1728 "2023-06-05T12:40:34Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![aaditmshah](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aaditmshah/32/602_2.png) [@aaditmshah](https://es.discourse.group/u/aaditmshah)\
**Post date:** [June 7, 2023, 4:57am UTC](https://es.discourse.group/t/monadic-functions/1728/5 "2023-06-07T04:57:06Z")

</div>

> [@theScottyJam](#):
>
> The use-case presented is to "flatten nested code caused by using monadic data types". The example solution presented is very similar to other functional languages, but I wonder if there's alternative syntax we could explore that might be more friendly for those who are less familiar with the scary "monad" word :).

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`.

```javascript
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](https://es.discourse.group/t/proposal-command-syntax/1716) 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.

> [@theScottyJam](#):
>
> The point is that, even though this classic monad-style syntax being presented is very common among functional languages, I'm not actually sure that that's the best way to solve the problem for a not-as-functional-language where many users aren't as familiar with the concept of a monad.

I don't buy that this syntax would be difficult for JavaScript programmers to learn for two reasons.

1. JavaScript programmers are already familiar with the `async…await` syntax, which is the same thing as `flat…map!` except that it's specialized for promises. An `await p` expression can be de-sugared to a `p.then` method call. Similarly, a `map! m` expression can be de-sugared to an `m.flatMap` method call. This shouldn't be too difficult for JavaScript programmers to grok.
2. JavaScript programmers don't need to understand the concept of a monad in order to use flat functions. They already know what the [`map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) method does, what the [`flat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) method does, and what the [`flatMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap) 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 a `flatMap` method.

---

_[View the full topic](https://es.discourse.group/t/monadic-functions/1728)._
