Closed jh3141 closed 6 years ago
Thanks for your suggestion. What's the use-case for these operations?
I commonly use them when fetching a group of related items from a database to perform an operation on, but the operation cannot progress if any of the items is missing.
For example, from my current project:
stationBlacklist.save (
Seq.seq (indexData.getItems()).crossApply (ItemIndexingData::getBlackListStations)
.map (rpiAndStationId -> Tuples.flatten2 (rpiAndStationId.map2 (stations::get))) // get the actual station from its ID, if possible
.transform (StreamUtils::flattenOptionalSeq) // lose the empty tuples relating to any missing stations
.map (StationItemBlacklist::fromTuple)
.toList ());
Am I understanding this correctly, your getBlackListStations()
method returns something allong the lines of a Stream<Optional<STATIONID>>
? That doesn't seem like a reasonable type to me. Why not just omit the stations that are not present and return Stream<STATIONID>
instead?
I'm not yet convinced that the "flattening" concept is a sufficiently reusable one to be added to the API. Besides, the number of methods to be added to the Tuples
class will either explode, or we'll limit this to some degree, e.g. like 2, but in that case, why bother?
In the snippet provided, stations::get
return Optional<Station>
, so the result of:
rpiAndStationId.map2 (stations::get)
is a Tuple2<RPItem, Optional<Station>>
-- as the next processing step requires a station, I need to then remove the items from the stream that don't contain a station; I do this in two steps by first moving the Optional<> to outside of the tuple, and then removing them from the stream before finally processing them.
stations::get
returnOptional<Station>
...rpiAndStationId.map2 (stations::get)
is aTuple2<RPItem, Optional<Station>>
I'm sorry would you mind posting all of your involved types in this issue, or in a gist? I'm finding it a bit hard to reason about your types right now. Alternatively, a more reduced, minimal example that does not involve any of your business logic might help, too.
Closing this as "won't fix". There isn't a clear enough use-case to justify the addition.
I've been finding the following set of methods for order 2 tuples quite handy, and I'd imagine they'd be pretty easy to extend via your code generator...