This commit is contained in:
Marvin Löbel 2013-06-06 22:34:50 +02:00
parent 070015468d
commit cac4891154
5 changed files with 31 additions and 27 deletions

View File

@ -207,19 +207,19 @@ mod test {
fn test_each() {
let mut e1: EnumSet<Foo> = EnumSet::empty();
assert_eq!(~[], iter::to_vec(|f| e1.each(f)))
assert_eq!(~[], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e1.each(f)))
e1.add(A);
assert_eq!(~[A], iter::to_vec(|f| e1.each(f)))
assert_eq!(~[A], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e1.each(f)))
e1.add(C);
assert_eq!(~[A,C], iter::to_vec(|f| e1.each(f)))
assert_eq!(~[A,C], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e1.each(f)))
e1.add(C);
assert_eq!(~[A,C], iter::to_vec(|f| e1.each(f)))
assert_eq!(~[A,C], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e1.each(f)))
e1.add(B);
assert_eq!(~[A,B,C], iter::to_vec(|f| e1.each(f)))
assert_eq!(~[A,B,C], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e1.each(f)))
}
///////////////////////////////////////////////////////////////////////////
@ -236,12 +236,12 @@ mod test {
e2.add(C);
let e_union = e1 | e2;
assert_eq!(~[A,B,C], iter::to_vec(|f| e_union.each(f)))
assert_eq!(~[A,B,C], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e_union.each(f)))
let e_intersection = e1 & e2;
assert_eq!(~[C], iter::to_vec(|f| e_intersection.each(f)))
assert_eq!(~[C], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e_intersection.each(f)))
let e_subtract = e1 - e2;
assert_eq!(~[A], iter::to_vec(|f| e_subtract.each(f)))
assert_eq!(~[A], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e_subtract.each(f)))
}
}

View File

@ -65,16 +65,6 @@ pub trait FromIter<T> {
pub fn from_iter(iter: &fn(f: &fn(T) -> bool) -> bool) -> Self;
}
// NOTE: This should be in vec but can't because of coherence
impl<T> FromIter<T> for ~[T]{
#[inline(always)]
pub fn from_iter(iter: &fn(f: &fn(T) -> bool) -> bool) -> ~[T] {
let mut v = ~[];
for iter |x| { v.push(x) }
v
}
}
/**
* Return true if `predicate` is true for any values yielded by an internal iterator.
*
@ -269,7 +259,7 @@ mod tests {
#[test]
fn test_from_iter() {
let xs: = ~[1, 2, 3];
let xs = ~[1, 2, 3];
let ys: ~[int] = do FromIter::from_iter |f| { xs.each(|x| f(*x)) };
assert_eq!(xs, ys);
}

View File

@ -19,9 +19,12 @@ implementing the `Iterator` trait.
use cmp;
use iter;
use iter::FromIter;
use iter::{FromIter, Times};
use num::{Zero, One};
use prelude::*;
use option::{Option, Some, None};
use ops::{Add, Mul};
use cmp::Ord;
use clone::Clone;
/// An interface for dealing with "external iterators". These types of iterators
/// can be resumed at any time as all state is stored internally as opposed to
@ -871,9 +874,9 @@ mod tests {
use uint;
#[test]
fn test_counter_to_vec() {
fn test_counter_from_iter() {
let mut it = Counter::new(0, 5).take(10);
let xs = iter::to_vec(|f| it.advance(f));
let xs: ~[int] = iter::FromIter::from_iter::<int, ~[int]>(|f| it.advance(f));
assert_eq!(xs, ~[0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
}
@ -904,7 +907,7 @@ mod tests {
fn test_filter_map() {
let mut it = Counter::new(0u, 1u).take(10)
.filter_map(|x: uint| if x.is_even() { Some(x*x) } else { None });
assert_eq!(it.to_vec(), ~[0*0, 2*2, 4*4, 6*6, 8*8]);
assert_eq!(it.collect::<~[uint]>(), ~[0*0, 2*2, 4*4, 6*6, 8*8]);
}
#[test]
@ -1065,7 +1068,7 @@ mod tests {
#[test]
fn test_collect() {
let a = [1, 2, 3, 4, 5];
let a = ~[1, 2, 3, 4, 5];
let b: ~[int] = a.iter().transform(|&x| x).collect();
assert_eq!(a, b);
}

View File

@ -14,7 +14,7 @@ Many programming languages have a 'prelude': a particular subset of the
libraries that come with the language. Every program imports the prelude by
default.
For example, it would be annoying to add `use io::println;` to every single
For example, it would be annoying to add `use std::io::println;` to every single
program, and the vast majority of Rust programs will wish to print to standard
output. Therefore, it makes sense to import it into every program.
@ -49,7 +49,8 @@ pub use hash::Hash;
pub use old_iter::{BaseIter, ReverseIter, MutableIter, ExtendedIter, EqIter};
pub use old_iter::{CopyableIter, CopyableOrderedIter, CopyableNonstrictIter};
pub use old_iter::{ExtendedMutableIter};
pub use iter::Times;
pub use iter::{Times, FromIter};
// FIXME: #5898 pub use iterator::{Iterator, IteratorUtil};
pub use num::{Num, NumCast};
pub use num::{Orderable, Signed, Unsigned, Round};
pub use num::{Algebraic, Trigonometric, Exponential, Hyperbolic};

View File

@ -20,6 +20,7 @@ use clone::Clone;
use old_iter::BaseIter;
use old_iter;
use iterator::Iterator;
use iter::FromIter;
use kinds::Copy;
use libc;
use old_iter::CopyableIter;
@ -2996,6 +2997,15 @@ impl<'self, T> Iterator<&'self mut T> for MutVecIterator<'self, T> {
}
}
impl<T> FromIter<T> for ~[T]{
#[inline(always)]
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 option::{None, Option, Some};