(no title)
lisper | 23 days ago
This is the kind of explanation that drives me absolutely batshit crazy because it is fundamentally at odds with:
> Do you understand "flatmap"? Good, that's literally all a monad is: a flatmappable.
So, I think I understand flatmap, assuming that this is what you mean:
https://www.w3schools.com/Jsref/jsref_array_flatmap.asp
But this has absolutely nothing to do with "certain things are supposed to happen after other things", and CANNOT POSSIBLY have anything to do with that. Flatmap is a purely functional concept, and in the context of things that are purely functional, nothing ever actually happens. That's the whole point of "functional" as a concept. It cleanly separates the result of a computation from the process used to produce that result.
So one of your "simple" explanations must be wrong.
anon291|23 days ago
And you are correct. Monads have nothing to do with sequencing (I mean any more than any other non commutative operator -- remember x^2 is not the same as 2^x)
Haskell handles sequencing by reducing to weak head normal form which is controlled by case matching. There is no connection to monads in general. The IO monad uses case matching in its implementation of flatmap to achieve a sensible ordering.
As for JavaScript flat map, a.flatMap(b).flatMap(c) is the same as a.flatMap(function (x) { return b(x).flatMap(c);}).
This is the same as promises: a.then(b).then(c) is the same as a.then(function (x) { return b(x).then(c)}).
Literally everything for which this is true forms a monad and the monad laws apply.
KPGv2|22 days ago
That is to say, then checks the type of the return value and then takes on map or flatmap behavior depending on whether the return value of the callback is a Promise or not.
lisper|22 days ago
a.M(b).M(c) = a.M(function(x){return b(x).M(c)})
So the next question is: why should I care about that pattern in particular?