diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2013-06-18 22:59:48 -0400 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2013-06-21 03:23:59 -0400 |
| commit | 883c966d5c6ba93bf8834543500b01f644ab362f (patch) | |
| tree | 9d4d22407fddfe40d3a452249886146a0ab003d3 /src/libstd | |
| parent | 49c74524e2c5a2a81ce4cbe2c50a507c0be9f24e (diff) | |
| download | rust-883c966d5c6ba93bf8834543500b01f644ab362f.tar.gz rust-883c966d5c6ba93bf8834543500b01f644ab362f.zip | |
vec: replace `position` with `iter().position_`
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/iterator.rs | 2 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 46 |
2 files changed, 5 insertions, 43 deletions
diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index 57f23d8e657..394066f1d4c 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -22,7 +22,7 @@ use iter::{FromIter, Times}; use num::{Zero, One}; use option::{Option, Some, None}; use ops::{Add, Mul}; -use cmp::{Ord, Eq}; +use cmp::Ord; use clone::Clone; /// An interface for dealing with "external iterators". These types of iterators diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index ad03d8fc4a0..ce5662c6d4b 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -19,7 +19,7 @@ use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater}; use clone::Clone; use old_iter::BaseIter; use old_iter; -use iterator::{Iterator}; +use iterator::{Iterator, IteratorUtil}; use iter::FromIter; use kinds::Copy; use libc; @@ -1107,18 +1107,7 @@ pub fn rfind_between<T:Copy>(v: &[T], /// Find the first index containing a matching value pub fn position_elem<T:Eq>(v: &[T], x: &T) -> Option<uint> { - position(v, |y| *x == *y) -} - -/** - * Find the first index matching some predicate - * - * Apply function `f` to each element of `v`. When function `f` returns true - * then an option containing the index is returned. If `f` matches no elements - * then none is returned. - */ -pub fn position<T>(v: &[T], f: &fn(t: &T) -> bool) -> Option<uint> { - position_between(v, 0u, v.len(), f) + v.iter().position_(|y| *x == *y) } /** @@ -3147,18 +3136,6 @@ mod tests { } #[test] - fn test_position() { - fn less_than_three(i: &int) -> bool { *i < 3 } - fn is_eighteen(i: &int) -> bool { *i == 18 } - - assert!(position([], less_than_three).is_none()); - - let v1 = ~[5, 4, 3, 2, 1]; - assert_eq!(position(v1, less_than_three), Some(3u)); - assert!(position(v1, is_eighteen).is_none()); - } - - #[test] fn test_position_between() { assert!(position_between([], 0u, 0u, f).is_none()); @@ -3234,8 +3211,8 @@ mod tests { fn g(xy: &(int, char)) -> bool { let (_x, y) = *xy; y == 'd' } let v = ~[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')]; - assert_eq!(position(v, f), Some(1u)); - assert!(position(v, g).is_none()); + assert_eq!(rposition(v, f), Some(3u)); + assert!(rposition(v, g).is_none()); } #[test] @@ -3880,21 +3857,6 @@ mod tests { #[test] #[ignore(windows)] #[should_fail] - fn test_position_fail() { - let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; - let mut i = 0; - do position(v) |_elt| { - if i == 2 { - fail!() - } - i += 0; - false - }; - } - - #[test] - #[ignore(windows)] - #[should_fail] fn test_rposition_fail() { let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; let mut i = 0; |
