Skip to content
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 custom nth_back to Skip #60454

Merged
merged 1 commit into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/libcore/iter/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,20 @@ impl<I> DoubleEndedIterator for Skip<I> where I: DoubleEndedIterator + ExactSize
}
}

#[inline]
fn nth_back(&mut self, n: usize) -> Option<I::Item> {
let len = self.len();
if n < len {
self.iter.nth_back(n)
} else {
Copy link
Contributor

@sinkuu sinkuu May 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case n >= self.len() && self.len() != 0, additionally you need to do self.iter.nth_back(self.len() - 1); (or whatever that takes self.len()-items from the back) since the underlying iterator could have side effects.

let mut v = vec![];
let mut it = [1, 2, 3, 4, 5]
    .iter()
    .cloned()
    .inspect(|x| v.push(*x))
    .skip(1);
assert_eq!(it.len(), 4);
assert_eq!(it.nth_back(4), None);
assert_eq!(v, vec![5, 4, 3, 2]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why let _ = and not just the call to nth_back?

Copy link
Contributor

@sinkuu sinkuu May 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was just for expressing intent to throw away the return value of nth_back, and I don't meant it is actually needed.

if len > 0 {
// consume the original iterator
self.iter.nth_back(len-1);
}
None

This comment was marked as resolved.

}
}

fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
{
Expand Down
34 changes: 34 additions & 0 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,40 @@ fn test_skip_try_folds() {
assert_eq!(iter.next_back(), Some(24));
}

#[test]
fn test_skip_nth_back() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the comment above was addressed; if disagree please add a test that demonstrates that the underlying iterator is still advanced.

(You could do that with inspect, or something like let mut it = ...; it.by_ref().skip(10).nth_back(100); assert_eq!(it.next_back(), ...);)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I didn't understand you meant that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're now calling self.len() three times, maybe it's better to store it in a variable first.

let xs = [0, 1, 2, 3, 4, 5];
let mut it = xs.iter().skip(2);
assert_eq!(it.nth_back(0), Some(&5));
assert_eq!(it.nth_back(1), Some(&3));
assert_eq!(it.nth_back(0), Some(&2));
assert_eq!(it.nth_back(0), None);

let ys = [2, 3, 4, 5];
let mut ity = ys.iter();
let mut it = xs.iter().skip(2);
assert_eq!(it.nth_back(1), ity.nth_back(1));
assert_eq!(it.clone().nth(0), ity.clone().nth(0));
assert_eq!(it.nth_back(0), ity.nth_back(0));
assert_eq!(it.clone().nth(0), ity.clone().nth(0));
assert_eq!(it.nth_back(0), ity.nth_back(0));
assert_eq!(it.clone().nth(0), ity.clone().nth(0));
assert_eq!(it.nth_back(0), ity.nth_back(0));
assert_eq!(it.clone().nth(0), ity.clone().nth(0));

let mut it = xs.iter().skip(2);
assert_eq!(it.nth_back(4), None);
assert_eq!(it.nth_back(0), None);

let mut it = xs.iter();
it.by_ref().skip(2).nth_back(3);
assert_eq!(it.next_back(), Some(&1));

let mut it = xs.iter();
it.by_ref().skip(2).nth_back(10);
assert_eq!(it.next_back(), Some(&1));
}

#[test]
fn test_take_try_folds() {
let f = &|acc, x| i32::checked_add(2*acc, x);
Expand Down