3 ms·
Whether to use a for loop or for of or forEach or map().filter() etc flatMap takes care of a lot of that
by nefitty 5y ago
Whether to use a for loop or for of or forEach or map().filter() etc
flatMap takes care of a lot of that
- yakshaving_jgt 5y agoWhy 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))
- wlamartin 5y agoThey 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.
- yakshaving_jgt 5y agoThen why not just use a fold?
- wlamartin 5y agoSure, 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#filtermap https://gcanti.github.io/fp-ts/modules/Array.ts.html#filterm...