question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add flatMap (also known as concatMap, mapcat, mapcons, ...)

See original GitHub issue

Often 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:closed
  • Created 8 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
benjamingrcommented, Nov 7, 2015

My opinion is that we should stop adding array-related methods to core and start planning how to take the existing ones out (preferably into a companion Stream library 😃

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.

2reactions
grantilacommented, Dec 15, 2015

Adding arbitrary array helpers for fun without actually solving problems regarding promises doesn’t make any sense at all.

Bluebird’s map, reduce and filter 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found