News for hackers. Stuff that matters.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by beaconstudios on Wednesday January 05, 2022 @10:51AM (#29810456)
probably because flatMap (also called chain) is an important array (or monads in general) function in functional programming. If anything, .flat() is the aberration.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by ReleaseCandidat on Wednesday January 05, 2022 @11:25AM (#29810979)
> If anything, .flat() is the aberration.

Same as `flatmap` (`bind`), `flat` is 'part' of a monad (as `join`).

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by beaconstudios on Wednesday January 05, 2022 @11:28AM (#29811031)
good point, I didn't think of that equivalence.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by contravariant on Wednesday January 05, 2022 @05:39PM (#29816143)
In mathematics it would be 'flat' that is the important one, not flatMap. You get 'flatMap' by just composing the monad multiplication 'flat' with the monad functor's mapping of arrows 'map'. That's mainly just to highlight the similarity with regular monoids though.

But in a program it usually makes sense to do the flattening in one go, since it avoids the need to do some possibly expensive intermediate calculations.