diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-08 14:07:55 +1000 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-09 02:22:23 +1000 |
| commit | 65c7c58c8f15189d2e4ca6b166e933c5fe2d0691 (patch) | |
| tree | 499bb3cc4eab7f6be17a218c1a3dc2ee077f5ea0 /src/libstd | |
| parent | ce4f63dcee8997f8d2881e6e3cf9e04db085bd64 (diff) | |
| download | rust-65c7c58c8f15189d2e4ca6b166e933c5fe2d0691.tar.gz rust-65c7c58c8f15189d2e4ca6b166e933c5fe2d0691.zip | |
std: remove {all*,any*,count} in favour of iterators
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/iterator.rs | 20 | ||||
| -rw-r--r-- | src/libstd/str.rs | 27 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 174 |
3 files changed, 26 insertions, 195 deletions
diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index 4ed82f63b39..309e207eaaa 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -326,7 +326,7 @@ pub trait IteratorUtil<A> { /// assert!(a.iter().all(|&x| *x > 0)); /// assert!(!a.iter().all(|&x| *x > 2)); /// ~~~ - fn all(&mut self, f: &fn(&A) -> bool) -> bool; + fn all(&mut self, f: &fn(A) -> bool) -> bool; /// Tests whether any element of an iterator satisfies the specified /// predicate. @@ -341,7 +341,7 @@ pub trait IteratorUtil<A> { /// assert!(it.any(|&x| *x == 3)); /// assert!(!it.any(|&x| *x == 3)); /// ~~~ - fn any(&mut self, f: &fn(&A) -> bool) -> bool; + fn any(&mut self, f: &fn(A) -> bool) -> bool; } /// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also @@ -462,14 +462,14 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T { fn count(&mut self) -> uint { self.fold(0, |cnt, _x| cnt + 1) } #[inline(always)] - fn all(&mut self, f: &fn(&A) -> bool) -> bool { - for self.advance |x| { if !f(&x) { return false; } } + fn all(&mut self, f: &fn(A) -> bool) -> bool { + for self.advance |x| { if !f(x) { return false; } } return true; } #[inline(always)] - fn any(&mut self, f: &fn(&A) -> bool) -> bool { - for self.advance |x| { if f(&x) { return true; } } + fn any(&mut self, f: &fn(A) -> bool) -> bool { + for self.advance |x| { if f(x) { return true; } } return false; } } @@ -1080,18 +1080,18 @@ mod tests { #[test] fn test_all() { let v = ~&[1, 2, 3, 4, 5]; - assert!(v.iter().all(|&x| *x < 10)); + assert!(v.iter().all(|&x| x < 10)); assert!(!v.iter().all(|&x| x.is_even())); - assert!(!v.iter().all(|&x| *x > 100)); + assert!(!v.iter().all(|&x| x > 100)); assert!(v.slice(0, 0).iter().all(|_| fail!())); } #[test] fn test_any() { let v = ~&[1, 2, 3, 4, 5]; - assert!(v.iter().any(|&x| *x < 10)); + assert!(v.iter().any(|&x| x < 10)); assert!(v.iter().any(|&x| x.is_even())); - assert!(!v.iter().any(|&x| *x > 100)); + assert!(!v.iter().any(|&x| x > 100)); assert!(!v.slice(0, 0).iter().any(|_| fail!())); } } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 8dc546ec4f8..d0345a3953e 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -2883,6 +2883,7 @@ impl<'self> Iterator<char> for StrCharIterator<'self> { #[cfg(test)] mod tests { + use iterator::IteratorUtil; use container::Container; use char; use option::Some; @@ -2977,7 +2978,7 @@ mod tests { let mut v = ~[]; for each_split_char(s, c) |s| { v.push(s.to_owned()) } debug!("split_byte to: %?", v); - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } t("abc.hello.there", '.', [~"abc", ~"hello", ~"there"]); t(".hello.there", '.', [~"", ~"hello", ~"there"]); @@ -2995,7 +2996,7 @@ mod tests { let mut v = ~[]; for each_split_char(s, c) |s| { v.push(s.to_owned()) } debug!("split_byte to: %?", v); - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } let data = "ประเทศไทย中华Việt Nam"; t(data, 'V', [~"ประเทศไทย中华", ~"iệt Nam"]); @@ -3010,7 +3011,7 @@ mod tests { for each_splitn_char(s, c, n) |s| { v.push(s.to_owned()) } debug!("split_byte to: %?", v); debug!("comparing vs. %?", u); - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } t("abc.hello.there", '.', 0u, [~"abc.hello.there"]); t("abc.hello.there", '.', 1u, [~"abc", ~"hello.there"]); @@ -3037,7 +3038,7 @@ mod tests { for each_splitn_char(s, c, n) |s| { v.push(s.to_owned()) } debug!("split_byte to: %?", v); debug!("comparing vs. %?", u); - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } t("ประเทศไทย中华Việt Nam", '华', 1u, [~"ประเทศไทย中", ~"Việt Nam"]); @@ -3055,7 +3056,7 @@ mod tests { for each_splitn_char(s, c, n) |s| { v.push(s.to_owned()) } debug!("split_byte to: %?", v); debug!("comparing vs. %?", u); - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } let data = "ประเทศไทย中华Việt Nam"; t(data, 'V', 1u, [~"ประเทศไทย中华", ~"iệt Nam"]); @@ -3069,7 +3070,7 @@ mod tests { let mut v = ~[]; for each_split_char_no_trailing(s, c) |s| { v.push(s.to_owned()) } debug!("split_byte to: %?", v); - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } t("abc.hello.there", '.', [~"abc", ~"hello", ~"there"]); t(".hello.there", '.', [~"", ~"hello", ~"there"]); @@ -3088,7 +3089,7 @@ mod tests { let mut v = ~[]; for each_split_char_no_trailing(s, c) |s| { v.push(s.to_owned()) } debug!("split_byte to: %?", v); - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } let data = "ประเทศไทย中华Việt Nam"; t(data, 'V', [~"ประเทศไทย中华", ~"iệt Nam"]); @@ -3100,7 +3101,7 @@ mod tests { fn t<'a>(s: &str, sep: &'a str, u: &[~str]) { let mut v = ~[]; for each_split_str(s, sep) |s| { v.push(s.to_owned()) } - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } t("--1233345--", "12345", [~"--1233345--"]); t("abc::hello::there", "::", [~"abc", ~"hello", ~"there"]); @@ -3124,7 +3125,7 @@ mod tests { fn t(s: &str, sepf: &fn(char) -> bool, u: &[~str]) { let mut v = ~[]; for each_split(s, sepf) |s| { v.push(s.to_owned()) } - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } t("ประเทศไทย中华Việt Nam", |cc| cc == '华', [~"ประเทศไทย中", ~"Việt Nam"]); @@ -3140,7 +3141,7 @@ mod tests { fn t(s: &str, sepf: &fn(char) -> bool, u: &[~str]) { let mut v = ~[]; for each_split_no_trailing(s, sepf) |s| { v.push(s.to_owned()) } - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } t("ประเทศไทย中华Việt Nam", |cc| cc == '华', [~"ประเทศไทย中", ~"Việt Nam"]); @@ -3159,7 +3160,7 @@ mod tests { fn t(s: &str, f: &fn(&str, &fn(&str) -> bool) -> bool, u: &[~str]) { let mut v = ~[]; for f(s) |s| { v.push(s.to_owned()) } - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } t(lf, each_line, [~"", ~"Mary had a little lamb", ~"Little lamb"]); @@ -3179,7 +3180,7 @@ mod tests { fn t(s: &str, f: &fn(&str, &fn(&str) -> bool) -> bool, u: &[~str]) { let mut v = ~[]; for f(s) |s| { v.push(s.to_owned()) } - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } let data = "\nMary had a little lamb\nLittle lamb\n"; @@ -3193,7 +3194,7 @@ mod tests { fn t(s: &str, i: uint, u: &[~str]) { let mut v = ~[]; for each_split_within(s, i) |s| { v.push(s.to_owned()) } - assert!(vec::all2(v, u, |a,b| a == b)); + assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b)); } t("", 0, []); t("", 15, []); diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index f34e1a91b69..29f2dba77a6 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -1124,80 +1124,12 @@ pub fn foldr<'a, T, U>(v: &'a [T], mut z: U, p: &fn(t: &'a T, u: U) -> U) -> U { return z; } -/** - * Return true if a predicate matches any elements - * - * If the vector contains no elements then false is returned. - */ -pub fn any<T>(v: &[T], f: &fn(t: &T) -> bool) -> bool { - for each(v) |elem| { if f(elem) { return true; } } - false -} - -/** - * Return true if a predicate matches any elements in both vectors. - * - * If the vectors contains no elements then false is returned. - */ -pub fn any2<T, U>(v0: &[T], v1: &[U], - f: &fn(a: &T, b: &U) -> bool) -> bool { - let v0_len = len(v0); - let v1_len = len(v1); - let mut i = 0u; - while i < v0_len && i < v1_len { - if f(&v0[i], &v1[i]) { return true; }; - i += 1u; - } - false -} - -/** - * Return true if a predicate matches all elements - * - * If the vector contains no elements then true is returned. - */ -pub fn all<T>(v: &[T], f: &fn(t: &T) -> bool) -> bool { - for each(v) |elem| { if !f(elem) { return false; } } - true -} - -/** - * Return true if a predicate matches all elements - * - * If the vector contains no elements then true is returned. - */ -pub fn alli<T>(v: &[T], f: &fn(uint, t: &T) -> bool) -> bool { - for eachi(v) |i, elem| { if !f(i, elem) { return false; } } - true -} - -/** - * Return true if a predicate matches all elements in both vectors. - * - * If the vectors are not the same size then false is returned. - */ -pub fn all2<T, U>(v0: &[T], v1: &[U], - f: &fn(t: &T, u: &U) -> bool) -> bool { - let v0_len = len(v0); - if v0_len != len(v1) { return false; } - let mut i = 0u; - while i < v0_len { if !f(&v0[i], &v1[i]) { return false; }; i += 1u; } - true -} - /// Return true if a vector contains an element with the given value pub fn contains<T:Eq>(v: &[T], x: &T) -> bool { for each(v) |elt| { if *x == *elt { return true; } } false } -/// Returns the number of elements that are equal to a given value -pub fn count<T:Eq>(v: &[T], x: &T) -> uint { - let mut cnt = 0u; - for each(v) |elt| { if *x == *elt { cnt += 1u; } } - cnt -} - /** * Search for the first element that matches a given predicate * @@ -2074,7 +2006,7 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] { * If the vector is empty, true is returned. */ fn alli(&self, f: &fn(uint, t: &T) -> bool) -> bool { - alli(*self, f) + self.iter().enumerate().all(|(i, t)| f(i,t)) } /** * Apply a function to each element of a vector and return a concatenation @@ -3559,33 +3491,6 @@ mod tests { } #[test] - fn test_any_and_all() { - assert!(any([1u, 2u, 3u], is_three)); - assert!(!any([0u, 1u, 2u], is_three)); - assert!(any([1u, 2u, 3u, 4u, 5u], is_three)); - assert!(!any([1u, 2u, 4u, 5u, 6u], is_three)); - - assert!(all([3u, 3u, 3u], is_three)); - assert!(!all([3u, 3u, 2u], is_three)); - assert!(all([3u, 3u, 3u, 3u, 3u], is_three)); - assert!(!all([3u, 3u, 0u, 1u, 2u], is_three)); - } - - #[test] - fn test_any2_and_all2() { - - assert!(any2([2u, 4u, 6u], [2u, 4u, 6u], is_equal)); - assert!(any2([1u, 2u, 3u], [4u, 5u, 3u], is_equal)); - assert!(!any2([1u, 2u, 3u], [4u, 5u, 6u], is_equal)); - assert!(any2([2u, 4u, 6u], [2u, 4u], is_equal)); - - assert!(all2([2u, 4u, 6u], [2u, 4u, 6u], is_equal)); - assert!(!all2([1u, 2u, 3u], [4u, 5u, 3u], is_equal)); - assert!(!all2([1u, 2u, 3u], [4u, 5u, 6u], is_equal)); - assert!(!all2([2u, 4u, 6u], [2u, 4u], is_equal)); - } - - #[test] fn test_zip_unzip() { let v1 = ~[1, 2, 3]; let v2 = ~[4, 5, 6]; @@ -4364,81 +4269,6 @@ mod tests { #[test] #[ignore(windows)] #[should_fail] - fn test_any_fail() { - let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; - let mut i = 0; - do any(v) |_elt| { - if i == 2 { - fail!() - } - i += 0; - false - }; - } - - #[test] - #[ignore(windows)] - #[should_fail] - fn test_any2_fail() { - let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; - let mut i = 0; - do any(v) |_elt| { - if i == 2 { - fail!() - } - i += 0; - false - }; - } - - #[test] - #[ignore(windows)] - #[should_fail] - fn test_all_fail() { - let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; - let mut i = 0; - do all(v) |_elt| { - if i == 2 { - fail!() - } - i += 0; - true - }; - } - - #[test] - #[ignore(windows)] - #[should_fail] - fn test_alli_fail() { - let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; - let mut i = 0; - do alli(v) |_i, _elt| { - if i == 2 { - fail!() - } - i += 0; - true - }; - } - - #[test] - #[ignore(windows)] - #[should_fail] - fn test_all2_fail() { - let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; - let mut i = 0; - do all2(v, v) |_elt1, _elt2| { - if i == 2 { - fail!() - } - i += 0; - true - }; - } - - #[test] - #[ignore(windows)] - #[should_fail] #[allow(non_implicitly_copyable_typarams)] fn test_find_fail() { let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; @@ -4617,7 +4447,7 @@ mod tests { #[test] fn test_mut_rev_iterator() { use iterator::*; - let mut xs = [1, 2, 3, 4, 5]; + let mut xs = [1u, 2, 3, 4, 5]; for xs.mut_rev_iter().enumerate().advance |(i,x)| { *x += i; } |
