Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

concatMap? #2710

Open
jbrains opened this issue Mar 1, 2022 · 2 comments
Open

concatMap? #2710

jbrains opened this issue Mar 1, 2022 · 2 comments

Comments

@jbrains
Copy link
Contributor

jbrains commented Mar 1, 2022

I'm at best an Advanced Beginner, so maybe this is something simple that I merely need to learn.

I am aggregating information for nested collections using flatMap(). I later want to generate sequential ID numbers for all the items in the resulting flattened collection. When I try to do this, my ID numbers are unique, but not sequential, and I'd like them to be. I'm made to understand that I want concatMap().

So far, I have learned that I can combine Stream.concat() with map(), but then we lose the beautiful pattern of

        return shiftResult.bowlerShiftResults().flatMap(bowlerShiftResult ->
                bowlerShiftResult.gameEntries().flatMap(bowlerShiftResultGameEntry ->
                        bowlerShiftResultGameEntry.game().numberedFrames().flatMap(numberedFrame ->
                                numberedFrame._1().numberedBallsThrown().map(numberedBallThrown ->
                                        new BallThrownDetails(shiftResult.shiftResultHeader().shiftName(), bowlerShiftResult.identificationRecord(), bowlerShiftResultGameEntry.gameNumber(), numberedFrame._2(), numberedBallThrown._2(), numberedBallThrown._1())))));

Either we need to put Stream.concat() at every level or we need to follow the nested map()s with an equal number of nested Stream.concat()s. I'm hoping for nicer.

So, can we have concatMap() in general (as in, is this reasonable and feasible)? Alternatively, is there some other nice (enough) way for me to do this with Vavr as it is today?

@alwins0n
Copy link

alwins0n commented Mar 5, 2022

Are you sure that the problem cannot be solved via zipWithIndex or zipWith?

@jbrains
Copy link
Contributor Author

jbrains commented Mar 6, 2022

Are you sure that the problem cannot be solved via zipWithIndex or zipWith?

Is this a riddle? How do these functions help me flatten a nested collection while preserving the iteration order of the items?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants