spotify / completable-futures

Utilities for working with futures in Java 8
Apache License 2.0
393 stars 51 forks source link

Implement joinMap(...) #57

Closed mbruggmann closed 4 years ago

mbruggmann commented 6 years ago

Description from README: joinMap is a stream collector that applies an asynchronous operation to a stream of values and collects the results into a map. This is similar to applying an asynchronous operation and using joinList, but retains the mapping from the original value.

There is probably a more optimized way of doing this but I thought I'd push it anyway since it was discussed.

codecov-io commented 6 years ago

Codecov Report

Merging #57 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##             master    #57   +/-   ##
=======================================
  Coverage       100%   100%           
- Complexity       69     74    +5     
=======================================
  Files             6      6           
  Lines           191    202   +11     
  Branches          9     10    +1     
=======================================
+ Hits            191    202   +11
Impacted Files Coverage Δ Complexity Δ
...n/java/com/spotify/futures/CompletableFutures.java 100% <100%> (ø) 46 <6> (+5) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update de76239...d244bd6. Read the comment docs.

dflemstr commented 6 years ago

Could we solve this generically? There are combinators in the standard library like collectingAndThen etc., similarly here we could build a combinator that would only focus on the "wait for all futures" part and send the resulting future completions to a different collector.

It probably makes sense to have this method as well as a special case; it just might be worth thinking about this for the long run.

mbruggmann commented 6 years ago

@dflemstr Not sure I fully grasp how the collector would work. Do you think we should investigate that before we add the joinMap collector to the public API?

dflemstr commented 6 years ago

I think so. Let us call this hypothetical combinator join(). I would rather do join(toMap(...)) and join(toList()) and join(toSet()) etc. instead of treating each use-case individually.

mattnworb commented 4 years ago

@mbruggmann still want this? 😀

mbruggmann commented 4 years ago

@mattnworb Guess I survived a while without it now, but if anyone feels like this is useful and worthwhile I can have a stab at cleaning it up?

dflemstr commented 4 years ago

I'll take a stab at creating what I wrote in my comment from 2018 :)

mbruggmann commented 4 years ago

Now that we have allAsMap (#70) I'll close this. If we want a collector version of it, we can add that as another PR and possibly revisit the API as @dflemstr suggested.