-
Notifications
You must be signed in to change notification settings - Fork 313
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
New Itertools::tail
#899
New Itertools::tail
#899
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #899 +/- ##
==========================================
+ Coverage 94.38% 94.50% +0.11%
==========================================
Files 48 48
Lines 6665 6807 +142
==========================================
+ Hits 6291 6433 +142
Misses 374 374 ☔ View full report in Codecov by Sentry. |
ea470e7
to
42cac22
Compare
I wonder if libcore would want a But we could then wonder about |
42cac22
to
a3557a7
Compare
I fused the iterator instead of checking |
src/lib.rs
Outdated
/// `.rev().take(n).collect_vec().into_iter().rev()` | ||
/// to have the same result without consuming the entire iterator. | ||
#[cfg(feature = "use_alloc")] | ||
fn tail(self, n: usize) -> VecIntoIter<Self::Item> |
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.
Pondering naming: would it be worth a name that more directly emphasizes the allocating or the approach? Like a too-long name might be collect_last_n
. I wonder how best to make it clear that this isn't lazy like take
.
Relatedly, I'm curious how people would expect this to behave and what trait bounds they'd expect. I often expect things looking at the end of an iterator to want DoubleEndedIterator
, but this is very much a "look, I need to look at the end but I don't have a double-ended iterator" kind of thing.
So I wonder if tail
would make more sense for something using the *_back
methods, and this version should have a longer name of some sort.
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.
We are not doing const-generics yet but I'm sometimes thinking about it.
match iter.next_chunk() { // I want this stabilized BTW.
Ok(arr) => {
/*update; rotate;*/
arr.into_iter()
}
Err(arr_iter) => arr_iter,
}
All that to say it would return core::array::IntoIter
and that VecIntoIter
seems the right type and I would expect a simple Vec
from collect_...
.
I'm also wondering if tail
is the best name but I did not find a better one.
a3557a7
to
6168c5c
Compare
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.
Hi @Philippe-Cholet, looks reasonable.
I approve this so that you can merge, but I'd appreciate a second pair of eyes, maybe @scottmcm?
d61185a
to
7ca66fd
Compare
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.
"Product" code looks good, though you probably want some test+doc updates before merging.
461329e
to
e9d8e90
Compare
I polished the commits and added @scottmcm as co-author. Is there a better name than Note for myself: File a rust issue for a possible method more optimized than EDIT: Discussion there. |
e9d8e90
to
716dbbb
Compare
716dbbb
to
ba8e675
Compare
The `.tail(1)` test is for code coverage.
If the iterator is exact sized, then `.collect()` finishes the work. More generally, if the size hint knows enough and `nth` is efficient, this might skip most of the iterator efficiently. In the tests, `.filter(..)` is there to ensure that `tail` can't leverage a precise `size_hint` to entirely skip the iteration after initial `.collect()`. Co-Authored-By: scottmcm <scottmcm@users.noreply.github.com>
`rotate_left` is more efficient on `VecDeque` than on a slice. `VecDeque::from(vec)` is O(1) on recent rust. Co-Authored-By: scottmcm <scottmcm@users.noreply.github.com>
ba8e675
to
b51f26a
Compare
…cap, r=<try> Add invariant to VecDeque::pop_* that len < cap if pop successful Similar to rust-lang#114370 for VecDeque instead of Vec. I initially come from rust-itertools/itertools#899 where we noticed that `pop_front;push_back;` was slower than expected so `@scottmcm` suggested I file an issue which lead to https://internals.rust-lang.org/t/vecdeque-pop-front-push-back/20483 where **kornel** mentionned rust-lang#114334 (fixed by rust-lang#114370). This is my first time with codegen tests, I based the test on what was done for Vec.
…e_cap, r=Nilstrieb Add invariant to VecDeque::pop_* that len < cap if pop successful Similar to rust-lang#114370 for VecDeque instead of Vec. I initially come from rust-itertools/itertools#899 where we noticed that `pop_front;push_back;` was slower than expected so `@scottmcm` suggested I file an issue which lead to https://internals.rust-lang.org/t/vecdeque-pop-front-push-back/20483 where **kornel** mentionned rust-lang#114334 (fixed by rust-lang#114370). This is my first time with codegen tests, I based the test on what was done for Vec.
…e_cap, r=Nilstrieb Add invariant to VecDeque::pop_* that len < cap if pop successful Similar to rust-lang#114370 for VecDeque instead of Vec. I initially come from rust-itertools/itertools#899 where we noticed that `pop_front;push_back;` was slower than expected so `@scottmcm` suggested I file an issue which lead to https://internals.rust-lang.org/t/vecdeque-pop-front-push-back/20483 where **kornel** mentionned rust-lang#114334 (fixed by rust-lang#114370). This is my first time with codegen tests, I based the test on what was done for Vec.
Rollup merge of rust-lang#123089 - Philippe-Cholet:vecdeque_pop_assume_cap, r=Nilstrieb Add invariant to VecDeque::pop_* that len < cap if pop successful Similar to rust-lang#114370 for VecDeque instead of Vec. I initially come from rust-itertools/itertools#899 where we noticed that `pop_front;push_back;` was slower than expected so `@scottmcm` suggested I file an issue which lead to https://internals.rust-lang.org/t/vecdeque-pop-front-push-back/20483 where **kornel** mentionned rust-lang#114334 (fixed by rust-lang#114370). This is my first time with codegen tests, I based the test on what was done for Vec.
Hi, after 80 merged PRs fixing, modifying, testing, benchmarking, specializing or whatever really... I just checked this is my first PR where I actually add a "brand new" feature myself. 🤣
PS from the future: The discussion also lead me to contribute a small optimization inside
VecDeque::pop_{back,front}
that will be released in rust 1.79.0. 😎