Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC: Add `Option::filter` to the standard library #2124
Conversation
LukasKalbertodt
added some commits
Aug 21, 2017
kennytm
reviewed
Aug 24, 2017
|
Just some minor nitpick ^_^ |
text/0000-option-filter.md Outdated
text/0000-option-filter.md Outdated
LukasKalbertodt
added some commits
Aug 24, 2017
scottmcm
added
the
T-libs
label
Aug 24, 2017
This comment has been minimized.
This comment has been minimized.
CAD97
commented
Aug 25, 2017
•
|
One minor downside: including |
This comment has been minimized.
This comment has been minimized.
|
In all fairness, |
This comment has been minimized.
This comment has been minimized.
egilburg
commented
Aug 28, 2017
•
|
Could add Or is this kind of function aliasing discouraged in Rust? It's pretty common in languages like Ruby, e.g. |
This comment has been minimized.
This comment has been minimized.
psFried
commented
Aug 30, 2017
|
There's been at least a few times that I've started to type |
This comment has been minimized.
This comment has been minimized.
Ryan1729
commented
Sep 1, 2017
•
|
I like this idea. If this is done then it seems like EDIT: Thought about it a bit more and I guess in the |
This comment has been minimized.
This comment has been minimized.
|
@Ryan1729 I thought about a fn parse(input: &str) -> Result<Ast, Error> {}
fn generate(ast: Ast) -> Result<String, Error> {}
parse(input)
.and_then(generate)Now I wanted to add another step which verifies the AST. It doesn't produce a result, but can produce an error. So the signature looks like this: fn check(ast: &Ast) -> Result<(), Error> {}Today, I think there is no better way to integrate it into my code than: parse(input)
.and_then(|ast| {
check(&ast).map(|_| ast)
})
.and_then(generate)So that's not particularly nice, but maybe ok-ish. I think I encountered this particular situation not nearly as often as situations in which |
This comment has been minimized.
This comment has been minimized.
|
@LukasKalbertodt If let string: Result<String, Error> = catch {
let ast = parse(input)?;
check(&ast)?;
generate(ast)?
}; |
LukasKalbertodt
referenced this pull request
Oct 3, 2017
Closed
Add `Option::filter()` according to RFC 2124 #44996
This comment has been minimized.
This comment has been minimized.
de-vri-es
commented
Oct 8, 2017
•
|
Would it make sense to call it
|
This comment has been minimized.
This comment has been minimized.
|
Haskell calls this method I guess |
This comment has been minimized.
This comment has been minimized.
SuperCuber
commented
Oct 22, 2017
|
I agree with the |
This comment has been minimized.
This comment has been minimized.
|
I tried to find the name for this method in other languages:
(if you comment with information about other languages, I will add those here) Comparing both suggestions (with all points I could come up with):
(again, if you raise other points below, I'll add them) |
This comment has been minimized.
This comment has been minimized.
|
@LukasKalbertodt In other words, |
This comment has been minimized.
This comment has been minimized.
|
We'd probably prefer "retain" over "filter" for consistency with other containers like |
This comment has been minimized.
This comment has been minimized.
|
@glaebhoerl Isn't |
This comment has been minimized.
This comment has been minimized.
|
Hmm, could be. But my assumption is that the reason it wasn't named "filter" is the ambiguity around whether that means "filter-in" or "filter-out", which applies equally here. (For the record, I find the name "retain" kind of awkward and might prefer "filter" myself despite the ambiguity, in a vacuum, but again, consistency.) @kennytm, the downvoting of comments without explanation feels kind of obnoxious to me, could you please write a comment instead if you have a disagreement? Thank you. |
This comment has been minimized.
This comment has been minimized.
|
@glaebhoerl A method/function named I think we should pick the function name most people would intuitively just write without checking any docs, and in this respect I think @LukasKalbertodt The general function for filtering iterables is called "filter" in python. While this is not the same as |
This comment has been minimized.
This comment has been minimized.
|
I agree in that personally I think "filter" should be taken to mean filter-in, but I'm also not a representative sample, because I first learned Does anyone know what the motivation behind the name |
This comment has been minimized.
This comment has been minimized.
|
@glaebhoerl I think
in that twitter feed is right. There might be a few anecdotal examples - but are they, put together, notable? Haskell, is hardly alone in defining filter as filter-in. Other languages where it is filter-in: There are a bunch of other languages where it is called I'd like an example of a language where it is filter-out. |
dtolnay
self-assigned this
Nov 8, 2017
dtolnay
approved these changes
Nov 8, 2017
|
Thanks for the RFC! This is a small enough addition that I think we can go ahead with an implementation. Please open a tracking issue in rust-lang/rust and send a PR containing the implementation, fully documented with rustdoc, and an #[unstable] attribute linking to the tracking issue -- see rust-lang/rust#44682 for an example. |
This comment has been minimized.
This comment has been minimized.
CAD97
commented
Nov 8, 2017
|
@dtolnay see rust-lang/rust#44996, which has been closed on hold until further discussion of the RFC:
|
This comment has been minimized.
This comment has been minimized.
|
@dtolnay As much as I'd like this RFC to be accepted immediately, I have to say: I don't think the name debate is settled, really. There are a few alternative names and I think Or do we want to discuss the naming issue after implementing and before stabilizing it? |
This comment has been minimized.
This comment has been minimized.
|
Thanks folks! It appears I was biased by landing on this RFC after trying to write In my experience we discuss naming before merging a new API, but only enough so that we don't get it blatantly wrong. Then we debate it more rigorously as part of FCP to stabilize. I think the logic there is that 1 month spent writing code against an API is more valuable toward deciding whether it is named correctly, compared to 1 month spent academizing about the name without writing code against it. One recent example of this is Other than the name, it seems like there is agreement that we want this functionality and the method signature in the RFC is the correct one. Is that your impression as well? @LukasKalbertodt do you feel that there could be strong arguments in favor of either name that haven't been voiced yet? Based on the existing arguments, what name are you leaning toward now and why? |
This comment has been minimized.
This comment has been minimized.
Yip.
I don't expect any more super important arguments, to be honest. And about my preference: I'm really not sure! I thought I'm absolutely fine with merging the RFC now and discussing the name on the tracking issue :) |
dtolnay
referenced this pull request
Nov 8, 2017
Closed
Tracking issue for Option::filter (feature `option_filter`) #45860
This comment has been minimized.
This comment has been minimized.
|
Sounds good to me! |
dtolnay
merged commit bedc7b2
into
rust-lang:master
Nov 8, 2017
LukasKalbertodt
referenced this pull request
Nov 8, 2017
Merged
Add `Option::filter()` according to RFC 2124 #45863
kennytm
added a commit
to kennytm/rust
that referenced
this pull request
Nov 10, 2017
This comment has been minimized.
This comment has been minimized.
vitiral
commented
Nov 15, 2017
|
@LukasKalbertodt the rendered link is now broken, can you fix it? Thanks! |
LukasKalbertodt commentedAug 24, 2017
•
edited by dtolnay
Add the method
Option::filter<P>(self, predicate: P) -> Option<T>to the standard library. This method makes it possible to easily throw away aSomevalue depending on a given predicate. The callopt.filter(p)is equivalent toopt.into_iter().filter(p).next().Rendered
I certainly know that there are plenty of active, more important RFCs. But since this RFC only introduces a tiny change to the standard library, I figured I should try writing it.
Fixes #1485