diff options
| author | bors <bors@rust-lang.org> | 2013-08-07 10:32:20 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-08-07 10:32:20 -0700 |
| commit | cdba212e7299f6bda752abbb9f887c51d96f7586 (patch) | |
| tree | 9d2ab6bb31a5b472120010e55fe439de4b94f3af /src/libstd/iterator.rs | |
| parent | 29ffbbaaa850d3f8fe1b35e3a63defe9206a3eb7 (diff) | |
| parent | 55f3d04101c214a13f7d47fbdb140c5c1126c41b (diff) | |
| download | rust-cdba212e7299f6bda752abbb9f887c51d96f7586.tar.gz rust-cdba212e7299f6bda752abbb9f887c51d96f7586.zip | |
auto merge of #8326 : thestinger/rust/iterator, r=alexcrichton
The `extra::iter` module wasn't actually included in `extra.rs` when it was moved from `std`... I assume no one is going to miss it.
Diffstat (limited to 'src/libstd/iterator.rs')
| -rw-r--r-- | src/libstd/iterator.rs | 35 |
1 files changed, 16 insertions, 19 deletions
diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index e86f4d7a85e..1be398966bb 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -313,7 +313,7 @@ pub trait IteratorUtil<A> { /// ~~~ {.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 @@ pub trait IteratorUtil<A> { /// ~~~ {.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)); @@ -1300,10 +1300,9 @@ pub struct Take<T> { impl<A, T: Iterator<A>> Iterator<A> for Take<T> { #[inline] fn next(&mut self) -> Option<A> { - let next = self.iter.next(); if self.n != 0 { self.n -= 1; - next + self.iter.next() } else { None } @@ -1512,12 +1511,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) @@ -1604,7 +1601,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]); } @@ -1622,7 +1619,7 @@ mod tests { } 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 { @@ -1634,7 +1631,7 @@ mod tests { #[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]); } @@ -1723,7 +1720,7 @@ mod tests { 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]); @@ -1770,13 +1767,13 @@ mod tests { #[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); } @@ -1838,7 +1835,7 @@ mod tests { #[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(); |
