vavr icon indicating copy to clipboard operation
vavr copied to clipboard

concatMap?

Open jbrains opened this issue 3 years ago • 2 comments

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?

jbrains avatar Mar 01 '22 01:03 jbrains

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

alwins0n avatar Mar 05 '22 10:03 alwins0n

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?

jbrains avatar Mar 06 '22 02:03 jbrains