Add flatMap (also known as concatMap, mapcat, mapcons, ...)
See original GitHub issueOften times I find myself returning an array of array of promises, and flattening it myself with a finishing reduce
. A contrived example:
getJSON('products.json')
// we receive an array of articles:
// [{}, {}, {}]
.then(articles => articles.map(getRecommendations))
// for each article, we receive an array of recommendations:
// [[{}, {}], [{}], [{}]]
// but we want it as a single array, so we need to concat them
.reduce(ret, recommends => ret.concat(recommends))
// we now have a flat array, yay!
// [{}, {}, {} ,{}]
.map(recommendation => recommendation.date);
In collections-land that’s known as flatMap, mapcat, concatMap and so forth.
Is it useful enough to include in core, or niche and simple enough that flattening be left to the user?
Issue Analytics
- State:
- Created 8 years ago
- Comments:11 (3 by maintainers)
Top Results From Across the Web
What is the difference between mapcat in Clojure and ...
In Clojure you have a function called mapcat in Clojure, which bears some similarity to flatmap in Scala. It is used to map...
Read more >mapcat - clojure.core | ClojureDocs - ClojureDocs
Takes any nested combination of sequential things (lists, vectors, etc.) and returns their content... Added by MicahElliott. Log in to add a see-also...
Read more >FlatMap operator - ReactiveX
The FlatMap operator transforms an Observable by applying a function that you specify to each item emitted by the source Observable, where that...
Read more >Understanding Map, FlatMap, SwitchMap and ConcatMap | by ...
FlatMap and ConcatMap work are pretty much the same. They merge items emitted by multiple Observables and returns a single Observable.
Read more >The equivalent of `flatMap` is not `flatten`. Semantically ...
Semantically `flatMap` is equivalent to `(comp flatten map)` – hence the name! – but Clojure does offer it as a single function named...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
99% of our users don’t care about how sound or how nice the theory or the types are. People care about usefulness.
In practice,
map
filter
and other methods are seeing a ton of use in bluebird. Removing them would be a spit in the face of our users telling them that theoretical niceness trumps actual usefulness.If abstract references (bind operator, or whatever @zenparsing is calling it today) make it in that changes my opinion since it would let us extend stuff from the outside.
Adding arbitrary array helpers for fun without actually solving problems regarding promises doesn’t make any sense at all.
Bluebird’s
map
,reduce
andfilter
functions are not just nice array helpers. After all, we already have this in ES5. Bluebird’s versions work with arrays of promises and values mixed, as well as with operators (the function to map, filter and reduce) which might return promises. This logic is non-trivial, and the support in bluebird (or in a support library) makes perfect sense.If this actually would respect promises in both levels - the outer and inner arrays - it could potentially be bluebird-material, but It would affect the interpretation of the
concurrency
option in non-obvious ways. In many situations you would like to have different weights to the concurrency of the outer and inner mapping jobs, and then all of a sudden it becomes a bit messy.