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

Implement find() on Chain iterators #33289

Merged
merged 1 commit into from May 2, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/libcore/iter/mod.rs
Expand Up @@ -541,6 +541,23 @@ impl<A, B> Iterator for Chain<A, B> where
}
}

#[inline]
fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item> where
P: FnMut(&Self::Item) -> bool,
{
match self.state {
ChainState::Both => match self.a.find(&mut predicate) {
None => {
self.state = ChainState::Back;
self.b.find(predicate)
}
v => v
},
ChainState::Front => self.a.find(predicate),
ChainState::Back => self.b.find(predicate),
}
}

#[inline]
fn last(self) -> Option<A::Item> {
match self.state {
Expand Down
13 changes: 13 additions & 0 deletions src/libcoretest/iter.rs
Expand Up @@ -133,6 +133,19 @@ fn test_iterator_chain_count() {
assert_eq!(zs.iter().chain(&ys).count(), 4);
}

#[test]
fn test_iterator_chain_find() {
let xs = [0, 1, 2, 3, 4, 5];
let ys = [30, 40, 50, 60];
let mut iter = xs.iter().chain(&ys);
assert_eq!(iter.find(|&&i| i == 4), Some(&4));
assert_eq!(iter.next(), Some(&5));
assert_eq!(iter.find(|&&i| i == 40), Some(&40));
assert_eq!(iter.next(), Some(&50));
assert_eq!(iter.find(|&&i| i == 100), None);
assert_eq!(iter.next(), None);
}

#[test]
fn test_filter_map() {
let it = (0..).step_by(1).take(10)
Expand Down