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
Added Option::reduce #87036
Added Option::reduce #87036
Conversation
r? @yaahc (rust-highfive has picked a reviewer for you, use r? to override) |
Bikeshed, this looks a lot like Iterator::reduce, so it could be named that way. |
I quite like that idea, very much open to changing it to that if others agree. |
library/core/src/option.rs
Outdated
/// assert_eq!(Some(2).or_zip_with(Some(3), |a, b| a + b), Some(5)); | ||
/// assert_eq!(Some(2).or_zip_with(None, |a, b| a + b), Some(2)); | ||
/// assert_eq!(None.or_zip_with(Some(3), |a, b| a + b), Some(3)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These do not look like real use cases to me. At a first glance, you are trying to sum these up if any of them has a value, and get None
when both of them do not. What does this achieve? You can either resolve this by responding to me or changing the example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've altered the example to hopefully make the use-case clearly. Regardless, it is only an example. It's difficult to demonstrate a real use-case in such a limited, controlled environment.
On the recommendation by @the8472 I've renamed this method to |
This comment has been minimized.
This comment has been minimized.
Looks like this is a duplicate of #87036 which we rejected last month. From the other thread:
I still feel the same way as I did when the last issue came up, but since this has come up twice in as many months perhaps we should re-evaluate. |
Thanks for the response. The function I'm proposing here does not do the same thing as I'm not sure which past issue you're referring to (I believe you might have accidentally linked to this one). |
Oops, I linked this PR instead of the other one: #84695
I'm not following how this can't be done with |
Ah, I forget that I still believe that it's a common enough occurrence to warrant a dedicated function, but I won't resurrect a debate that's been so recently discussed (when I opened the issue, I didn't search for "reduce" due to the aforementioned name change). Thanks for the clarification! |
I've made a micro crate that provides the function, in case anyone needs it too: |
This PR adds a function that I and others have needed on a regular occurrence (more frequently than at least half of those on
Option
already) that does not have a concise inline alternative, nor can it seemingly be composed from existing functions onOption
.I've found this function to be particularly useful in the context of compiler development when one has to resolve two potentially non-existent values together, although it crops up in many other contexts. For example, two error values can be resolved like so:
This expands to:
(I've expanded this example for the sake of clarity)
Additionally, although not implemented in this PR, there is another, similar function that crops up almost as frequently:
However, this is trivially emulated with
a.into_iter().reduce(b, f)
.