From 0fc99f1997be9323bb75826c478094e74797ddf9 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 30 Jun 2013 20:12:04 +0200 Subject: [PATCH 1/2] Add an EnumSetIterator and EnumSet::iter --- src/librustc/util/enum_set.rs | 92 +++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 9 deletions(-) diff --git a/src/librustc/util/enum_set.rs b/src/librustc/util/enum_set.rs index 2bdb6583b2304..7f29ce98b3ec1 100644 --- a/src/librustc/util/enum_set.rs +++ b/src/librustc/util/enum_set.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::iterator::Iterator; #[deriving(Eq, IterBytes)] pub struct EnumSet { @@ -73,6 +74,10 @@ impl EnumSet { } return true; } + + pub fn iter(&self) -> EnumSetIterator { + EnumSetIterator::new(self.bits) + } } impl Sub, EnumSet> for EnumSet { @@ -93,6 +98,39 @@ impl BitAnd, EnumSet> for EnumSet { } } +pub struct EnumSetIterator { + priv index: uint, + priv bits: uint, +} + +impl EnumSetIterator { + fn new(bits: uint) -> EnumSetIterator { + EnumSetIterator { index: 0, bits: bits } + } +} + +impl Iterator for EnumSetIterator { + fn next(&mut self) -> Option { + if (self.bits == 0) { + return None; + } + + while (self.bits & 1) == 0 { + self.index += 1; + self.bits >>= 1; + } + let elem = CLike::from_uint(self.index); + self.index += 1; + self.bits >>= 1; + Some(elem) + } + + fn size_hint(&self) -> (Option, Option) { + let exact = Some(self.bits.population_count()); + (exact, exact) + } +} + #[cfg(test)] mod test { @@ -199,25 +237,58 @@ mod test { } /////////////////////////////////////////////////////////////////////////// - // each + // iterator + + #[test] + fn test_iterator() { + let mut e1: EnumSet = EnumSet::empty(); + + let elems: ~[Foo] = e1.iter().collect(); + assert_eq!(~[], elems) + + e1.add(A); + let elems: ~[Foo] = e1.iter().collect(); + assert_eq!(~[A], elems) + + e1.add(C); + let elems: ~[Foo] = e1.iter().collect(); + assert_eq!(~[A,C], elems) + + e1.add(C); + let elems: ~[Foo] = e1.iter().collect(); + assert_eq!(~[A,C], elems) + + e1.add(B); + let elems: ~[Foo] = e1.iter().collect(); + assert_eq!(~[A,B,C], elems) + } + + fn collect(e: EnumSet) -> ~[Foo] { + let mut elems = ~[]; + e.each(|elem| { + elems.push(elem); + true + }); + elems + } #[test] fn test_each() { let mut e1: EnumSet = EnumSet::empty(); - assert_eq!(~[], iter::FromIter::from_iter::(|f| e1.each(f))) + assert_eq!(~[], collect(e1)) e1.add(A); - assert_eq!(~[A], iter::FromIter::from_iter::(|f| e1.each(f))) + assert_eq!(~[A], collect(e1)) e1.add(C); - assert_eq!(~[A,C], iter::FromIter::from_iter::(|f| e1.each(f))) + assert_eq!(~[A,C], collect(e1)) e1.add(C); - assert_eq!(~[A,C], iter::FromIter::from_iter::(|f| e1.each(f))) + assert_eq!(~[A,C], collect(e1)) e1.add(B); - assert_eq!(~[A,B,C], iter::FromIter::from_iter::(|f| e1.each(f))) + assert_eq!(~[A,B,C], collect(e1)) } /////////////////////////////////////////////////////////////////////////// @@ -234,12 +305,15 @@ mod test { e2.add(C); let e_union = e1 | e2; - assert_eq!(~[A,B,C], iter::FromIter::from_iter::(|f| e_union.each(f))) + let elems: ~[Foo] = e_union.iter().collect(); + assert_eq!(~[A,B,C], elems) let e_intersection = e1 & e2; - assert_eq!(~[C], iter::FromIter::from_iter::(|f| e_intersection.each(f))) + let elems: ~[Foo] = e_intersection.iter().collect(); + assert_eq!(~[C], elems) let e_subtract = e1 - e2; - assert_eq!(~[A], iter::FromIter::from_iter::(|f| e_subtract.each(f))) + let elems: ~[Foo] = e_subtract.iter().collect(); + assert_eq!(~[A], elems) } } From 3fe05a987c0d1684b1172ca29c9a9ad79852419e Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 29 Jun 2013 04:09:58 +0200 Subject: [PATCH 2/2] Move most iter functionality to extra, fixes #7343 --- src/libextra/iter.rs | 328 ++++++++++++++++++++++++++++++++++ src/librustc/middle/ty.rs | 3 +- src/librustc/util/enum_set.rs | 21 ++- src/libstd/iter.rs | 307 +------------------------------ src/libstd/prelude.rs | 2 +- src/libstd/vec.rs | 10 -- 6 files changed, 345 insertions(+), 326 deletions(-) create mode 100644 src/libextra/iter.rs diff --git a/src/libextra/iter.rs b/src/libextra/iter.rs new file mode 100644 index 0000000000000..ad8dcf98317a4 --- /dev/null +++ b/src/libextra/iter.rs @@ -0,0 +1,328 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +/*! Composable internal iterators + +Internal iterators are functions implementing the protocol used by the `for` loop. + +An internal iterator takes `fn(...) -> bool` as a parameter, with returning `false` used to signal +breaking out of iteration. The adaptors in the module work with any such iterator, not just ones +tied to specific traits. For example: + +~~~ {.rust} +println(iter::to_vec(|f| uint::range(0, 20, f)).to_str()); +~~~ + +An external iterator object implementing the interface in the `iterator` module can be used as an +internal iterator by calling the `advance` method. For example: + +~~~ {.rust} +let xs = [0u, 1, 2, 3, 4, 5]; +let ys = [30, 40, 50, 60]; +let mut it = xs.iter().chain(ys.iter()); +for it.advance |&x: &uint| { + println(x.to_str()); +} +~~~ + +Internal iterators provide a subset of the functionality of an external iterator. It's not possible +to interleave them to implement algorithms like `zip`, `union` and `merge`. However, they're often +much easier to implement. + +*/ + +use std::vec; +use std::cmp::Ord; +use std::option::{Option, Some, None}; +use std::num::{One, Zero}; +use std::ops::{Add, Mul}; + +#[allow(missing_doc)] +pub trait FromIter { + /// Build a container with elements from an internal iterator. + /// + /// # Example: + /// + /// ~~~ {.rust} + /// let xs = ~[1, 2, 3]; + /// let ys: ~[int] = do FromIter::from_iter |f| { xs.iter().advance(|x| f(*x)) }; + /// assert_eq!(xs, ys); + /// ~~~ + pub fn from_iter(iter: &fn(f: &fn(T) -> bool) -> bool) -> Self; +} + +/** + * Return true if `predicate` is true for any values yielded by an internal iterator. + * + * Example: + * + * ~~~ {.rust} + * let xs = ~[1u, 2, 3, 4, 5]; + * assert!(any(|&x: &uint| x > 2, |f| xs.iter().advance(f))); + * assert!(!any(|&x: &uint| x > 5, |f| xs.iter().advance(f))); + * ~~~ + */ +#[inline] +pub fn any(predicate: &fn(T) -> bool, + iter: &fn(f: &fn(T) -> bool) -> bool) -> bool { + for iter |x| { + if predicate(x) { + return true; + } + } + return false; +} + +/** + * Return true if `predicate` is true for all values yielded by an internal iterator. + * + * # Example: + * + * ~~~ {.rust} + * assert!(all(|&x: &uint| x < 6, |f| uint::range(1, 6, f))); + * assert!(!all(|&x: &uint| x < 5, |f| uint::range(1, 6, f))); + * ~~~ + */ +#[inline] +pub fn all(predicate: &fn(T) -> bool, + iter: &fn(f: &fn(T) -> bool) -> bool) -> bool { + // If we ever break, iter will return false, so this will only return true + // if predicate returns true for everything. + iter(|x| predicate(x)) +} + +/** + * Return the first element where `predicate` returns `true`. Return `None` if no element is found. + * + * # Example: + * + * ~~~ {.rust} + * let xs = ~[1u, 2, 3, 4, 5, 6]; + * assert_eq!(*find(|& &x: & &uint| x > 3, |f| xs.iter().advance(f)).unwrap(), 4); + * ~~~ + */ +#[inline] +pub fn find(predicate: &fn(&T) -> bool, + iter: &fn(f: &fn(T) -> bool) -> bool) -> Option { + for iter |x| { + if predicate(&x) { + return Some(x); + } + } + None +} + +/** + * Return the largest item yielded by an iterator. Return `None` if the iterator is empty. + * + * # Example: + * + * ~~~ {.rust} + * let xs = ~[8, 2, 3, 1, -5, 9, 11, 15]; + * assert_eq!(max(|f| xs.iter().advance(f)).unwrap(), &15); + * ~~~ + */ +#[inline] +pub fn max(iter: &fn(f: &fn(T) -> bool) -> bool) -> Option { + let mut result = None; + for iter |x| { + match result { + Some(ref mut y) => { + if x > *y { + *y = x; + } + } + None => result = Some(x) + } + } + result +} + +/** + * Return the smallest item yielded by an iterator. Return `None` if the iterator is empty. + * + * # Example: + * + * ~~~ {.rust} + * let xs = ~[8, 2, 3, 1, -5, 9, 11, 15]; + * assert_eq!(max(|f| xs.iter().advance(f)).unwrap(), &-5); + * ~~~ + */ +#[inline] +pub fn min(iter: &fn(f: &fn(T) -> bool) -> bool) -> Option { + let mut result = None; + for iter |x| { + match result { + Some(ref mut y) => { + if x < *y { + *y = x; + } + } + None => result = Some(x) + } + } + result +} + +/** + * Reduce an iterator to an accumulated value. + * + * # Example: + * + * ~~~ {.rust} + * assert_eq!(fold(0i, |f| int::range(1, 5, f), |a, x| *a += x), 10); + * ~~~ + */ +#[inline] +pub fn fold(start: T, iter: &fn(f: &fn(U) -> bool) -> bool, f: &fn(&mut T, U)) -> T { + let mut result = start; + for iter |x| { + f(&mut result, x); + } + result +} + +/** + * Reduce an iterator to an accumulated value. + * + * `fold_ref` is usable in some generic functions where `fold` is too lenient to type-check, but it + * forces the iterator to yield borrowed pointers. + * + * # Example: + * + * ~~~ {.rust} + * fn product>(iter: &fn(f: &fn(&T) -> bool) -> bool) -> T { + * fold_ref(One::one::(), iter, |a, x| *a = a.mul(x)) + * } + * ~~~ + */ +#[inline] +pub fn fold_ref(start: T, iter: &fn(f: &fn(&U) -> bool) -> bool, f: &fn(&mut T, &U)) -> T { + let mut result = start; + for iter |x| { + f(&mut result, x); + } + result +} + +/** + * Return the sum of the items yielding by an iterator. + * + * # Example: + * + * ~~~ {.rust} + * let xs: ~[int] = ~[1, 2, 3, 4]; + * assert_eq!(do sum |f| { xs.iter().advance(f) }, 10); + * ~~~ + */ +#[inline] +pub fn sum>(iter: &fn(f: &fn(&T) -> bool) -> bool) -> T { + fold_ref(Zero::zero::(), iter, |a, x| *a = a.add(x)) +} + +/** + * Return the product of the items yielded by an iterator. + * + * # Example: + * + * ~~~ {.rust} + * let xs: ~[int] = ~[1, 2, 3, 4]; + * assert_eq!(do product |f| { xs.iter().advance(f) }, 24); + * ~~~ + */ +#[inline] +pub fn product>(iter: &fn(f: &fn(&T) -> bool) -> bool) -> T { + fold_ref(One::one::(), iter, |a, x| *a = a.mul(x)) +} + +impl FromIter for ~[T]{ + #[inline] + pub fn from_iter(iter: &fn(f: &fn(T) -> bool) -> bool) -> ~[T] { + let mut v = ~[]; + for iter |x| { v.push(x) } + v + } +} + +#[cfg(test)] +mod tests { + use super::*; + use prelude::*; + + use int; + use uint; + + #[test] + fn test_from_iter() { + let xs = ~[1, 2, 3]; + let ys: ~[int] = do FromIter::from_iter |f| { xs.iter().advance(|x| f(*x)) }; + assert_eq!(xs, ys); + } + + #[test] + fn test_any() { + let xs = ~[1u, 2, 3, 4, 5]; + assert!(any(|&x: &uint| x > 2, |f| xs.iter().advance(f))); + assert!(!any(|&x: &uint| x > 5, |f| xs.iter().advance(f))); + } + + #[test] + fn test_all() { + assert!(all(|x: uint| x < 6, |f| uint::range(1, 6, f))); + assert!(!all(|x: uint| x < 5, |f| uint::range(1, 6, f))); + } + + #[test] + fn test_find() { + let xs = ~[1u, 2, 3, 4, 5, 6]; + assert_eq!(*find(|& &x: & &uint| x > 3, |f| xs.iter().advance(f)).unwrap(), 4); + } + + #[test] + fn test_max() { + let xs = ~[8, 2, 3, 1, -5, 9, 11, 15]; + assert_eq!(max(|f| xs.iter().advance(f)).unwrap(), &15); + } + + #[test] + fn test_min() { + let xs = ~[8, 2, 3, 1, -5, 9, 11, 15]; + assert_eq!(min(|f| xs.iter().advance(f)).unwrap(), &-5); + } + + #[test] + fn test_fold() { + assert_eq!(fold(0i, |f| int::range(1, 5, f), |a, x| *a += x), 10); + } + + #[test] + fn test_sum() { + let xs: ~[int] = ~[1, 2, 3, 4]; + assert_eq!(do sum |f| { xs.iter().advance(f) }, 10); + } + + #[test] + fn test_empty_sum() { + let xs: ~[int] = ~[]; + assert_eq!(do sum |f| { xs.iter().advance(f) }, 0); + } + + #[test] + fn test_product() { + let xs: ~[int] = ~[1, 2, 3, 4]; + assert_eq!(do product |f| { xs.iter().advance(f) }, 24); + } + + #[test] + fn test_empty_product() { + let xs: ~[int] = ~[]; + assert_eq!(do product |f| { xs.iter().advance(f) }, 1); + } +} diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 4ed21d73f3e78..a78143c33cdfb 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -29,7 +29,6 @@ use util::enum_set::{EnumSet, CLike}; use std::cast; use std::cmp; use std::hashmap::{HashMap, HashSet}; -use std::iter; use std::ops; use std::ptr::to_unsafe_ptr; use std::to_bytes; @@ -1752,7 +1751,7 @@ pub struct TypeContents { impl TypeContents { pub fn meets_bounds(&self, cx: ctxt, bbs: BuiltinBounds) -> bool { - iter::all(|bb| self.meets_bound(cx, bb), |f| bbs.each(f)) + bbs.iter().all(|bb| self.meets_bound(cx, bb)) } pub fn meets_bound(&self, cx: ctxt, bb: BuiltinBound) -> bool { diff --git a/src/librustc/util/enum_set.rs b/src/librustc/util/enum_set.rs index 7f29ce98b3ec1..f9bd7a3508edb 100644 --- a/src/librustc/util/enum_set.rs +++ b/src/librustc/util/enum_set.rs @@ -135,7 +135,6 @@ impl Iterator for EnumSetIterator { mod test { use std::cast; - use std::iter; use util::enum_set::*; @@ -237,7 +236,7 @@ mod test { } /////////////////////////////////////////////////////////////////////////// - // iterator + // iter / each #[test] fn test_iterator() { @@ -263,15 +262,6 @@ mod test { assert_eq!(~[A,B,C], elems) } - fn collect(e: EnumSet) -> ~[Foo] { - let mut elems = ~[]; - e.each(|elem| { - elems.push(elem); - true - }); - elems - } - #[test] fn test_each() { let mut e1: EnumSet = EnumSet::empty(); @@ -291,6 +281,15 @@ mod test { assert_eq!(~[A,B,C], collect(e1)) } + fn collect(e: EnumSet) -> ~[Foo] { + let mut elems = ~[]; + e.each(|elem| { + elems.push(elem); + true + }); + elems + } + /////////////////////////////////////////////////////////////////////////// // operators diff --git a/src/libstd/iter.rs b/src/libstd/iter.rs index 4e598a4aa1cd6..2092ae588d01d 100644 --- a/src/libstd/iter.rs +++ b/src/libstd/iter.rs @@ -8,316 +8,19 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -/*! Composable internal iterators - -Internal iterators are functions implementing the protocol used by the `for` loop. - -An internal iterator takes `fn(...) -> bool` as a parameter, with returning `false` used to signal -breaking out of iteration. The adaptors in the module work with any such iterator, not just ones -tied to specific traits. For example: +/*! Times trait ~~~ {.rust} -println(iter::to_vec(|f| uint::range(0, 20, f)).to_str()); +use iter::Times; +let ten = 10 as uint; +let mut accum = 0; +for ten.times { accum += 1; } ~~~ -An external iterator object implementing the interface in the `iterator` module can be used as an -internal iterator by calling the `advance` method. For example: - -~~~ {.rust} -let xs = [0u, 1, 2, 3, 4, 5]; -let ys = [30, 40, 50, 60]; -let mut it = xs.iter().chain(ys.iter()); -for it.advance |&x: &uint| { - println(x.to_str()); -} -~~~ - -Internal iterators provide a subset of the functionality of an external iterator. It's not possible -to interleave them to implement algorithms like `zip`, `union` and `merge`. However, they're often -much easier to implement. - */ -use cmp::Ord; -use option::{Option, Some, None}; -use num::{One, Zero}; -use ops::{Add, Mul}; - #[allow(missing_doc)] pub trait Times { fn times(&self, it: &fn() -> bool) -> bool; } -#[allow(missing_doc)] -pub trait FromIter { - /// Build a container with elements from an internal iterator. - /// - /// # Example: - /// - /// ~~~ {.rust} - /// let xs = ~[1, 2, 3]; - /// let ys: ~[int] = do FromIter::from_iter |f| { xs.iter().advance(|x| f(*x)) }; - /// assert_eq!(xs, ys); - /// ~~~ - pub fn from_iter(iter: &fn(f: &fn(T) -> bool) -> bool) -> Self; -} - -/** - * Return true if `predicate` is true for any values yielded by an internal iterator. - * - * Example: - * - * ~~~ {.rust} - * let xs = ~[1u, 2, 3, 4, 5]; - * assert!(any(|&x: &uint| x > 2, |f| xs.iter().advance(f))); - * assert!(!any(|&x: &uint| x > 5, |f| xs.iter().advance(f))); - * ~~~ - */ -#[inline] -pub fn any(predicate: &fn(T) -> bool, - iter: &fn(f: &fn(T) -> bool) -> bool) -> bool { - for iter |x| { - if predicate(x) { - return true; - } - } - return false; -} - -/** - * Return true if `predicate` is true for all values yielded by an internal iterator. - * - * # Example: - * - * ~~~ {.rust} - * assert!(all(|&x: &uint| x < 6, |f| uint::range(1, 6, f))); - * assert!(!all(|&x: &uint| x < 5, |f| uint::range(1, 6, f))); - * ~~~ - */ -#[inline] -pub fn all(predicate: &fn(T) -> bool, - iter: &fn(f: &fn(T) -> bool) -> bool) -> bool { - // If we ever break, iter will return false, so this will only return true - // if predicate returns true for everything. - iter(|x| predicate(x)) -} - -/** - * Return the first element where `predicate` returns `true`. Return `None` if no element is found. - * - * # Example: - * - * ~~~ {.rust} - * let xs = ~[1u, 2, 3, 4, 5, 6]; - * assert_eq!(*find(|& &x: & &uint| x > 3, |f| xs.iter().advance(f)).unwrap(), 4); - * ~~~ - */ -#[inline] -pub fn find(predicate: &fn(&T) -> bool, - iter: &fn(f: &fn(T) -> bool) -> bool) -> Option { - for iter |x| { - if predicate(&x) { - return Some(x); - } - } - None -} - -/** - * Return the largest item yielded by an iterator. Return `None` if the iterator is empty. - * - * # Example: - * - * ~~~ {.rust} - * let xs = ~[8, 2, 3, 1, -5, 9, 11, 15]; - * assert_eq!(max(|f| xs.iter().advance(f)).unwrap(), &15); - * ~~~ - */ -#[inline] -pub fn max(iter: &fn(f: &fn(T) -> bool) -> bool) -> Option { - let mut result = None; - for iter |x| { - match result { - Some(ref mut y) => { - if x > *y { - *y = x; - } - } - None => result = Some(x) - } - } - result -} - -/** - * Return the smallest item yielded by an iterator. Return `None` if the iterator is empty. - * - * # Example: - * - * ~~~ {.rust} - * let xs = ~[8, 2, 3, 1, -5, 9, 11, 15]; - * assert_eq!(max(|f| xs.iter().advance(f)).unwrap(), &-5); - * ~~~ - */ -#[inline] -pub fn min(iter: &fn(f: &fn(T) -> bool) -> bool) -> Option { - let mut result = None; - for iter |x| { - match result { - Some(ref mut y) => { - if x < *y { - *y = x; - } - } - None => result = Some(x) - } - } - result -} - -/** - * Reduce an iterator to an accumulated value. - * - * # Example: - * - * ~~~ {.rust} - * assert_eq!(fold(0i, |f| int::range(1, 5, f), |a, x| *a += x), 10); - * ~~~ - */ -#[inline] -pub fn fold(start: T, iter: &fn(f: &fn(U) -> bool) -> bool, f: &fn(&mut T, U)) -> T { - let mut result = start; - for iter |x| { - f(&mut result, x); - } - result -} - -/** - * Reduce an iterator to an accumulated value. - * - * `fold_ref` is usable in some generic functions where `fold` is too lenient to type-check, but it - * forces the iterator to yield borrowed pointers. - * - * # Example: - * - * ~~~ {.rust} - * fn product>(iter: &fn(f: &fn(&T) -> bool) -> bool) -> T { - * fold_ref(One::one::(), iter, |a, x| *a = a.mul(x)) - * } - * ~~~ - */ -#[inline] -pub fn fold_ref(start: T, iter: &fn(f: &fn(&U) -> bool) -> bool, f: &fn(&mut T, &U)) -> T { - let mut result = start; - for iter |x| { - f(&mut result, x); - } - result -} - -/** - * Return the sum of the items yielding by an iterator. - * - * # Example: - * - * ~~~ {.rust} - * let xs: ~[int] = ~[1, 2, 3, 4]; - * assert_eq!(do sum |f| { xs.iter().advance(f) }, 10); - * ~~~ - */ -#[inline] -pub fn sum>(iter: &fn(f: &fn(&T) -> bool) -> bool) -> T { - fold_ref(Zero::zero::(), iter, |a, x| *a = a.add(x)) -} - -/** - * Return the product of the items yielded by an iterator. - * - * # Example: - * - * ~~~ {.rust} - * let xs: ~[int] = ~[1, 2, 3, 4]; - * assert_eq!(do product |f| { xs.iter().advance(f) }, 24); - * ~~~ - */ -#[inline] -pub fn product>(iter: &fn(f: &fn(&T) -> bool) -> bool) -> T { - fold_ref(One::one::(), iter, |a, x| *a = a.mul(x)) -} - -#[cfg(test)] -mod tests { - use super::*; - use prelude::*; - - use int; - use uint; - - #[test] - fn test_from_iter() { - let xs = ~[1, 2, 3]; - let ys: ~[int] = do FromIter::from_iter |f| { xs.iter().advance(|x| f(*x)) }; - assert_eq!(xs, ys); - } - - #[test] - fn test_any() { - let xs = ~[1u, 2, 3, 4, 5]; - assert!(any(|&x: &uint| x > 2, |f| xs.iter().advance(f))); - assert!(!any(|&x: &uint| x > 5, |f| xs.iter().advance(f))); - } - - #[test] - fn test_all() { - assert!(all(|x: uint| x < 6, |f| uint::range(1, 6, f))); - assert!(!all(|x: uint| x < 5, |f| uint::range(1, 6, f))); - } - - #[test] - fn test_find() { - let xs = ~[1u, 2, 3, 4, 5, 6]; - assert_eq!(*find(|& &x: & &uint| x > 3, |f| xs.iter().advance(f)).unwrap(), 4); - } - - #[test] - fn test_max() { - let xs = ~[8, 2, 3, 1, -5, 9, 11, 15]; - assert_eq!(max(|f| xs.iter().advance(f)).unwrap(), &15); - } - - #[test] - fn test_min() { - let xs = ~[8, 2, 3, 1, -5, 9, 11, 15]; - assert_eq!(min(|f| xs.iter().advance(f)).unwrap(), &-5); - } - - #[test] - fn test_fold() { - assert_eq!(fold(0i, |f| int::range(1, 5, f), |a, x| *a += x), 10); - } - - #[test] - fn test_sum() { - let xs: ~[int] = ~[1, 2, 3, 4]; - assert_eq!(do sum |f| { xs.iter().advance(f) }, 10); - } - - #[test] - fn test_empty_sum() { - let xs: ~[int] = ~[]; - assert_eq!(do sum |f| { xs.iter().advance(f) }, 0); - } - - #[test] - fn test_product() { - let xs: ~[int] = ~[1, 2, 3, 4]; - assert_eq!(do product |f| { xs.iter().advance(f) }, 24); - } - - #[test] - fn test_empty_product() { - let xs: ~[int] = ~[]; - assert_eq!(do product |f| { xs.iter().advance(f) }, 1); - } -} diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index 13d19b276f59e..58db740167f32 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -47,7 +47,7 @@ pub use cmp::{Eq, ApproxEq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Great pub use char::Char; pub use container::{Container, Mutable, Map, Set}; pub use hash::Hash; -pub use iter::{Times, FromIter}; +pub use iter::{Times}; pub use iterator::{Iterator, IteratorUtil, OrdIterator}; pub use num::{Num, NumCast}; pub use num::{Orderable, Signed, Unsigned, Round}; diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index cff4ac10145c7..bbb7de69c4042 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -19,7 +19,6 @@ use cmp; use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater}; use clone::Clone; use iterator::{FromIterator, Iterator, IteratorUtil}; -use iter::FromIter; use kinds::Copy; use libc; use num::Zero; @@ -2311,15 +2310,6 @@ pub struct VecMutRevIterator<'self, T> { } iterator!{impl VecMutRevIterator -> &'self mut T, -1} -impl FromIter for ~[T]{ - #[inline] - pub fn from_iter(iter: &fn(f: &fn(T) -> bool) -> bool) -> ~[T] { - let mut v = ~[]; - for iter |x| { v.push(x) } - v - } -} - #[cfg(stage0)] impl> FromIterator for ~[A] { pub fn from_iterator(iterator: &mut T) -> ~[A] {