Re:FP techniques that will help you write better JavaScript
by bluefirebrand on Friday February 11, 2022 @09:39AM
(#30301970)
I see people use JavaScript Array.map a lot when they aren't doing anything with the returned array, which I find kind of irritating.
So if you're using map sensibly, no there's nothing wrong. But I don't like the dogma I read a lot which is "use map always" because it leads to inexperience developers using it in the wrong circumstances.
Re:FP techniques that will help you write better JavaScript
by Akronymus on Friday February 11, 2022 @10:08AM
(#30302278)
Does JS have something like array.iter?
Re:FP techniques that will help you write better JavaScript
by yakshaving_jgt on Friday February 11, 2022 @05:03AM
(#30299486)
What are you struggling with when trying to skip elements?
> Is it bad to use map?
No.
Re:FP techniques that will help you write better JavaScript
by nefitty on Friday February 11, 2022 @05:25AM
(#30299664)
Whether to use a for loop or for of or forEach or map().filter() etc
flatMap takes care of a lot of that
Re:FP techniques that will help you write better JavaScript
by yakshaving_jgt on Friday February 11, 2022 @05:29AM
(#30299692)
Why are you using a “trick” to skip an element in some structure when mapping a function over it?
For posterity (and forgive me if this JavaScript syntax is inaccurate; I don't write it so much these days)…
xs.map(a => shouldBeSkipped ? a : f(a))
Re:FP techniques that will help you write better JavaScript
by wlamartin on Friday February 11, 2022 @09:34AM
(#30301917)
They don't want to keep the unmapped value in the resulting collection at all. flatMap allows for removal of an element in one traverse, unlike filter+map with eager behaviour.
Re:FP techniques that will help you write better JavaScript
by yakshaving_jgt on Friday February 11, 2022 @09:38AM
(#30301964)
Then why not just use a fold?
Re:FP techniques that will help you write better JavaScript
by wlamartin on Friday February 11, 2022 @10:09AM
(#30302287)
Sure, there's many ways to skin a cat. Given the option, I wouldn't choose either of these, and instead use something like filterMap[1] which I think conveys intent better than a fold or flatMap.
Btw, I'm assuming that the original map questioner wasn't solely using flatMap for side-effectful iteration, which reading again, I'm a bit suspicious about.
1: https://gcanti.github.io/fp-ts/modules/Array.ts.html#filterm...
Is it bad to use map?