3 ms·
Not what I meant, as one can obviously also filter or map in a reduce block, but maybe one needs to do a sort afterwards.
by petre 4y ago
Not what I meant, as one can obviously also filter or map in a reduce block, but maybe one needs to do a sort afterwards.
- afavour 4y ago> one can obviously also filter or map in a reduce block Sure but you can't filter in a map, or map in a filter.
- leipert 4y agoReason why I like flatMap. It’s like map with a simple shortcut for filtering.
- dpkirchner 4y agoYou can filter in a compactMap (called just compact in lodash, apparently), FWIW. Not that this negates your point.
- dragonwriter 4y agosure. Reduce is the generic array -> anything operation, flatMap is the generic array -> array operation (and, as such, a special case of reduce), and map and filter are specialized array -> array operations (and, as such, special cases of flatMap and, transitively, of reduce.) So you can do both map and filter as part of a reduce or flatMap, but not either as part of the other. (You may be able to get close by doing ugly things in a map/filter that abuses the second and third arguments, but I don't think you can get a general map out of filter or vice versa, and you shouldn't do the kind of things you'd need to do to get close, just use flatMap.)