diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2013-05-18 04:27:58 -0400 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2013-05-18 04:35:58 -0400 |
| commit | ea8a55b821460ef7b3b58052f99673674fca8f96 (patch) | |
| tree | 877fd1a08cd4edace99ecbefe8f4408c73f094b8 | |
| parent | a9c465ce1f1caaae58a31c57f665217ee58bb876 (diff) | |
| download | rust-ea8a55b821460ef7b3b58052f99673674fca8f96.tar.gz rust-ea8a55b821460ef7b3b58052f99673674fca8f96.zip | |
iterator: make nth and last return Option
There isn't a way to take the length of any iterator, so failing on a zero length would make these much less useful.
| -rw-r--r-- | src/libcore/iterator.rs | 50 |
1 files changed, 15 insertions, 35 deletions
diff --git a/src/libcore/iterator.rs b/src/libcore/iterator.rs index 68281526e6f..65c36b122c7 100644 --- a/src/libcore/iterator.rs +++ b/src/libcore/iterator.rs @@ -48,8 +48,8 @@ pub trait IteratorUtil<A> { #[cfg(not(stage0))] fn advance(&mut self, f: &fn(A) -> bool) -> bool; fn to_vec(self) -> ~[A]; - fn nth(&mut self, n: uint) -> A; - fn last(&mut self) -> A; + fn nth(&mut self, n: uint) -> Option<A>; + fn last(&mut self) -> Option<A>; fn fold<B>(&mut self, start: B, f: &fn(B, A) -> B) -> B; fn count(&mut self) -> uint; fn all(&mut self, f: &fn(&A) -> bool) -> bool; @@ -154,30 +154,24 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T { return v; } - /// Get `n`th element of an iterator. + /// Return the `n`th item yielded by an iterator. #[inline(always)] - fn nth(&mut self, n: uint) -> A { - let mut i = n; + fn nth(&mut self, mut n: uint) -> Option<A> { loop { match self.next() { - Some(x) => { if i == 0 { return x; }} - None => { fail!("cannot get %uth element", n) } + Some(x) => if n == 0 { return Some(x) }, + None => return None } - i -= 1; + n -= 1; } } - // Get last element of an iterator. - // - // If the iterator have an infinite length, this method won't return. + /// Return the last item yielded by an iterator. #[inline(always)] - fn last(&mut self) -> A { - let mut elm = match self.next() { - Some(x) => x, - None => fail!("cannot get last element") - }; - for self.advance |e| { elm = e; } - return elm; + fn last(&mut self) -> Option<A> { + let mut last = None; + for self.advance |x| { last = Some(x); } + last } /// Reduce an iterator to an accumulated value @@ -679,29 +673,15 @@ mod tests { fn test_iterator_nth() { let v = &[0, 1, 2, 3, 4]; for uint::range(0, v.len()) |i| { - assert_eq!(v.iter().nth(i), &v[i]); + assert_eq!(v.iter().nth(i).unwrap(), &v[i]); } } #[test] - #[should_fail] - fn test_iterator_nth_fail() { - let v = &[0, 1, 2, 3, 4]; - v.iter().nth(5); - } - - #[test] fn test_iterator_last() { let v = &[0, 1, 2, 3, 4]; - assert_eq!(v.iter().last(), &4); - assert_eq!(v.slice(0, 1).iter().last(), &0); - } - - #[test] - #[should_fail] - fn test_iterator_last_fail() { - let v: &[uint] = &[]; - v.iter().last(); + assert_eq!(v.iter().last().unwrap(), &4); + assert_eq!(v.slice(0, 1).iter().last().unwrap(), &0); } #[test] |
