←back to thread

A list is a monad

(alexyorke.github.io)
153 points polygot | 3 comments | | HN request time: 0.638s | source
Show context
gr4vityWall ◴[] No.44446943[source]
I think the most intuitive description for a monad I've ever seen is 'flatMappable'.

Context: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Usually articles that describe them in a very Math-y way go above my head. But the definition above was immediately clear (I saw it on HN).

I think this article is a bit more approachable than others I've read, but it still gets very confusing near the end.

replies(2): >>44446957 #>>44447634 #
aadhavans ◴[] No.44446957[source]
Could you elaborate on that? What does 'flatMappable' mean in this context?
replies(2): >>44447160 #>>44448214 #
1. IshKebab ◴[] No.44447160[source]
This is a good explanation:

https://users.scala-lang.org/t/what-is-a-monad-in-scala/4169

It's like... what would you call all types that have a .write() method? Writable right? What would you call all types that have a .dispose() method? Disposable. What would you call all types that have a .flatMap() method? Monad obviously.

replies(1): >>44450100 #
2. skybrian ◴[] No.44450100[source]
That’s because flatMap() is a good name for a particular list operation, but it’s not generic enough to be a good name for the corresponding monad operation.

I’m not sure there is a good name for the monad operation. Sometimes it’s called ‘bind’ but what does it bind?

I suppose you could call it ‘then’ like when working with Promises.

replies(1): >>44451916 #
3. still_grokking ◴[] No.44451916[source]
Scala's new effect library Kyo just uses `map`. See:

https://getkyo.io/#/?id=the-quotpendingquot-type-lt

All pure values are automatically lifted into the Kyo monad, so `map` is effectively `flatMap`.

From the linked docs:

> This unique property removes the need to juggle between map and flatMap. All values are automatically promoted to a Kyo computation with zero pending effects, enabling you to focus on your application logic rather than the intricacies of effect handling.

In the end it makes a lot of sense I think. What you do is manipulating values inside some wrapper. Whether this wrapper is a monad or not should not matter. Just do something with the value(s) inside, and that's mapping.