提交 40d11a54 编写于 作者: D Daniel Micay

iterator: rename `Counter::new` to `count`

to match the convention used by `range`, since `iterator::count` is
already namespaced enough and won't be ambiguous
上级 c13f4f39
......@@ -696,7 +696,7 @@ fn test_from_iterator() {
let u: ~[int] = deq.iter().transform(|&x| x).collect();
assert_eq!(u, v);
let mut seq = iterator::Counter::new(0u, 2).take_(256);
let mut seq = iterator::count(0u, 2).take_(256);
let deq: RingBuf<uint> = seq.collect();
for (i, &x) in deq.iter().enumerate() {
assert_eq!(2*i, x);
......
......@@ -313,7 +313,7 @@ fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option<B>)
/// ~~~ {.rust}
/// let xs = [2u, 3];
/// let ys = [0u, 1, 0, 1, 2];
/// let mut it = xs.iter().flat_map_(|&x| Counter::new(0u, 1).take_(x));
/// let mut it = xs.iter().flat_map_(|&x| count(0u, 1).take_(x));
/// // Check that `it` has the same elements as `ys`
/// let mut i = 0;
/// for x: uint in it {
......@@ -351,7 +351,7 @@ fn flat_map_<'r, B, U: Iterator<B>>(self, f: &'r fn(A) -> U)
/// ~~~ {.rust}
/// use std::iterator::Counter;
///
/// for i in Counter::new(0, 10) {
/// for i in count(0, 10) {
/// printfln!("%d", i);
/// }
/// ~~~
......@@ -723,7 +723,7 @@ pub trait MultiplicativeIterator<A> {
/// use std::iterator::Counter;
///
/// fn factorial(n: uint) -> uint {
/// Counter::new(1u, 1).take_while(|&i| i <= n).product()
/// count(1u, 1).take_while(|&i| i <= n).product()
/// }
/// assert!(factorial(0) == 1);
/// assert!(factorial(1) == 1);
......@@ -790,7 +790,7 @@ pub trait ClonableIterator {
/// # Example
///
/// ~~~ {.rust}
/// let a = Counter::new(1,1).take_(1);
/// let a = count(1,1).take_(1);
/// let mut cy = a.cycle();
/// assert_eq!(cy.next(), Some(1));
/// assert_eq!(cy.next(), Some(1));
......@@ -1526,12 +1526,10 @@ pub struct Counter<A> {
step: A
}
impl<A> Counter<A> {
/// Creates a new counter with the specified start/step
#[inline]
pub fn new(start: A, step: A) -> Counter<A> {
Counter{state: start, step: step}
}
/// Creates a new counter with the specified start/step
#[inline]
pub fn count<A>(start: A, step: A) -> Counter<A> {
Counter{state: start, step: step}
}
/// A range of numbers from [0, N)
......@@ -1618,7 +1616,7 @@ mod tests {
#[test]
fn test_counter_from_iter() {
let mut it = Counter::new(0, 5).take_(10);
let mut it = count(0, 5).take_(10);
let xs: ~[int] = FromIterator::from_iterator(&mut it);
assert_eq!(xs, ~[0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
}
......@@ -1636,7 +1634,7 @@ fn test_iterator_chain() {
}
assert_eq!(i, expected.len());
let ys = Counter::new(30u, 10).take_(4);
let ys = count(30u, 10).take_(4);
let mut it = xs.iter().transform(|&x| x).chain_(ys);
let mut i = 0;
for x in it {
......@@ -1648,7 +1646,7 @@ fn test_iterator_chain() {
#[test]
fn test_filter_map() {
let mut it = Counter::new(0u, 1u).take_(10)
let mut it = count(0u, 1u).take_(10)
.filter_map(|x| if x.is_even() { Some(x*x) } else { None });
assert_eq!(it.collect::<~[uint]>(), ~[0*0, 2*2, 4*4, 6*6, 8*8]);
}
......@@ -1737,7 +1735,7 @@ fn add(old: &mut int, new: &uint) -> Option<float> {
fn test_iterator_flat_map() {
let xs = [0u, 3, 6];
let ys = [0u, 1, 2, 3, 4, 5, 6, 7, 8];
let mut it = xs.iter().flat_map_(|&x| Counter::new(x, 1).take_(3));
let mut it = xs.iter().flat_map_(|&x| count(x, 1).take_(3));
let mut i = 0;
for x in it {
assert_eq!(x, ys[i]);
......@@ -1784,13 +1782,13 @@ fn count(st: &mut uint) -> Option<uint> {
#[test]
fn test_cycle() {
let cycle_len = 3;
let it = Counter::new(0u, 1).take_(cycle_len).cycle();
let it = count(0u, 1).take_(cycle_len).cycle();
assert_eq!(it.size_hint(), (uint::max_value, None));
for (i, x) in it.take_(100).enumerate() {
assert_eq!(i % cycle_len, x);
}
let mut it = Counter::new(0u, 1).take_(0).cycle();
let mut it = count(0u, 1).take_(0).cycle();
assert_eq!(it.size_hint(), (0, Some(0)));
assert_eq!(it.next(), None);
}
......@@ -1852,7 +1850,7 @@ fn test_iterator_min() {
#[test]
fn test_iterator_size_hint() {
let c = Counter::new(0, 1);
let c = count(0, 1);
let v = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let v2 = &[10, 11, 12];
let vi = v.iter();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册