vavr
vavr copied to clipboard
concatMap?
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?
Are you sure that the problem cannot be solved via zipWithIndex or zipWith?
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?