-
Notifications
You must be signed in to change notification settings - Fork 317
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 is_first/is_last helpers to Position #1019
base: master
Are you sure you want to change the base?
Conversation
They're just convenience method, but avoid having to dereference individual variants, and may mitigate missing the `Only` case.
Originally proposed by bluss (#169 (comment)) Do make the first/last cases clearer and somewhat less error prone e.g. use itertools::Itertools;
let sentence = "The quick brown fox.";
let words = sentence.split(' ');
for (pos, word) in words.with_position() {
if let itertools::Position::Last | itertools::Position::Only = pos {
print!("{}", word);
}
} becomes use itertools::Itertools;
let sentence = "The quick brown fox.";
let words = sentence.split(' ');
for (pos, word) in words.with_position() {
if pos.is_last() {
print!("{}", word);
}
} |
Hi there, I'm unsure that an But then, Thus, I'm reluctant to merge this. @jswrenn Opinions? |
To me that is the entire point, it's a predicate telling you whether an element is the first / last of the sequence regardless of the sequence's dimensions, so you don't need to know / care about the I was reminder of this inconvenience upon stumbling (back) on https://stackoverflow.com/a/76223155/8182118 |
In this case, I think the chainability of the method tips the scales towards the shorthand being compelling. (As opposed to |
I appreciate the reasoning, but it goes against Rust's convention:
I.e. (Notably, |
@jswrenn What does "chainability" exactly mean? Is it that e.g. for an |
I mean that I'm more amenable to shorthands when they preserve chainability than when they merely safe keystrokes. |
They're just convenience method, but avoid having to dereference individual variants, and may mitigate missing the
Only
case.