-
Notifications
You must be signed in to change notification settings - Fork 168
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
Add Seq.reducingMap() and Seq.reducePartially() methods #338
Comments
Thank you very much for your suggestion. The functionality seems to be covered by |
Thank you for your answer. I'll try to highlight the differences to make it more clear:
Maybe the examples that I provided are too weak, because the conditions in those examples always test only one value (either Example 2 (
My real use case is much more complex than simple string concatenation, but it effectively uses something like |
indeed, this could be improved with additional overloads
OK I think I understand your use case now. I'm not convinced about the naming of course, but I see the value of such a method. Will think about it this week. |
Did you find the time to think about it? :) I'll understand if you want to wait until more people request this feature, though. In the meantime, here's some explanation and further proposals concerning naming: 1)
|
No, sorry
Yes, definitely. I already regret 1-2 prematurely added features to jOOλ. I prefer not to add more things without enough demand from the community
One reason why I'm often not so quick (or keen) to respond to these things is because of the complexity of the feature. It takes quite a bit of time to think about why this could be useful (not just how it works). This is, to me, a strong indicator that it is not generally useful.
Sure. |
On several occasions, I've been looking for a method that would be an inverse of
Stream.flatMap
. I noticed that other people do too - here's an example question on StackOverflow.However, there are quite many ways to define it (especially the criteria for such an inverse operation). Finally I've decided to stick to the well-understood reduction semantics, and I've come up with the following two method signatures (names to be discussed) that I'd like to propose for
Seq
:reducePartially
works likeStream.reduce(BinaryOperator)
, only it yields and restarts the reduction ifconditionalAccumulator
returns an emptyOptional
:reducePartially
is similar toStreamEx.collapse(BiPredicate,BinaryOperator)
, with the main difference being that the first argument toconditionalAccumulator
is the result of previous reduction (exactly as inreduce
).reducingMap
works likeStream.reduce(U, BiFunction, BinaryOperator)
, only it yields and restarts the reduction ifconditionalAccumulator
returns an emptyOptional
, and it has a different mechanism for providing initialU
s (instead ofU identity
we have aninitialMapper
that converts the firstT
to the initialU
):reducingMap
bears some resemblance toStreamEx.collapse(BiPredicate,Collector)
, but - again - it's much more a "reduce" than a "mark" + "merge".Here are a few examples of how it is supposed to work:
Seq.reducePartially
Example 1: Merge words ending with hyphens with the following word:
(a, b) -> a.endsWith("-") ? Optional.of(withoutLastChar(a) + b) : Optional.empty();
"Sample", "hyp-", "hen-", "ated", "text"
"Sample", "hyphenated", "text"
Seq.reducingMap
Example 1: Join integers using a semicolon until
0
occurs:String::valueOf
(String a, int b) -> b != 0 ? Optional.of(a + ";" + b) : Optional.empty();
7, 8, 0, 1, 5, 7, 8, 0, 3, 1, 7, 9, 0, 0, 5
"7;8", "0;1;5;7;8", "0;3;1;7;9", "0", "0;5"
I have this implemented (using a custom, lazy
Spliterator
) so I could provide a PR if the feature gets accepted.The text was updated successfully, but these errors were encountered: