News for hackers. Stuff that matters.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by Waterluvian on Wednesday January 05, 2022 @10:40AM (#29810311)
I've always found `flatMap` to be interesting because it feels like a convenience function, combining .map(fn).flat() into one function. It's interesting because JS doesn't really have a lot of convenience functions that are this shallow (ie. that provide just minimal cleanup compared to the functions they're wrapping).

Is there some specific reason that `flatMap` made the cut?

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.

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by Tuna-Fish on Wednesday January 05, 2022 @12:45PM (#29812248)
For a lot of common cases (where the returned lists from the fn are short, but the complete list is long), a combined flatMap can be optimized to be much faster than doing the functions one by one.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by HellzStormer on Wednesday January 05, 2022 @10:52AM (#29810467)
Small mistake, and maybe that's why this function exists, but it's actually combines `.map(fn).flat(1)`.

I think conceptually, `flatMap` is a `map` where each iteration can return multiple values (or none). So it feels quite more powerful than just `map` and is a quite common convenience function.

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by mstade on Wednesday January 05, 2022 @12:01PM (#29811562)
I think the depth argument of flat defaults to 1, so it'd be the same anyway. Not sure why the default is 1, I'd prefer Infinity so it'd always flatten everything by default, but maybe there are some good reasons for it.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by Waterluvian on Wednesday January 05, 2022 @02:44PM (#29813896)
I think it should be explicit without a default.

But in the real world, if we must have a default, I think it should be 1, not infinity.

Infinity has a frustrating fail state, in my opinion: you might be flattening arrays that weren't meant to be flattened. And now you've got a cluster!@#$ of data to look at and reason about.

The fail state of 1 is that it doesn't flatten enough but you have the original structure in your 1-level flattened output. So you're far more likely to make heads and tails of how much more flattening you need.

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by mstade on Wednesday January 05, 2022 @03:45PM (#29814659)
Arguably the same argument works the other way around as well, that in thinking flat recursively flattens all arrays of arrays you get confused when it doesn't. (I've definitely run across that footgun.)

For me, I just find that in almost all cases I've used flat it's been with Infinity as the depth value. It seems to me as well that most of the time you'd probably either want 1 or Infinity, not 5 or 12 or whatever, so perhaps it would've even been better to just have two functions: flat and flatDeep (or some such, naming is hard) the latter of which would default to Infinity but allow different depths as well.

Probably reads better than just flat as well, e.g. `.flatDeep(5)` rather than `.flat(5)`. Oh well, we're stuck with this now so it's all an academic exercise anyway, but I'd be curious to know the rationale of the design. Maybe I'll wade through the spec repo one day to see if there's any discussion about it.

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by Osiris on Wednesday January 05, 2022 @06:38PM (#29816867)
In what circumstances is this common?

I've been a developer for 15 years and I have rarely found a need for flatMap.

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by jmath on Wednesday January 05, 2022 @10:49AM (#29810426)
perhaps it’s a case of “names are fun”