Skip to content

Commit d1dc65c

Browse files
author
Jakub Wieczorek
committed
---
yaml --- r: 123834 b: refs/heads/auto c: ed54162 h: refs/heads/master v: v3
1 parent 0997103 commit d1dc65c

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: 55cf6d723c40bf720d7d9f9ed3a5833caa8faf1a
16+
refs/heads/auto: ed54162e86cd00b7a4ced8957aac7f56897f6fb5
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/libcore/iter.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ the rest of the rust manuals.
6464
6565
*/
6666

67+
use clone::Clone;
6768
use cmp;
69+
use cmp::{PartialEq, PartialOrd, Ord};
70+
use mem;
6871
use num::{Zero, One, CheckedAdd, CheckedSub, Saturating, ToPrimitive, Int};
69-
use option::{Option, Some, None};
7072
use ops::{Add, Mul, Sub};
71-
use cmp::{PartialEq, PartialOrd, Ord};
72-
use clone::Clone;
73+
use option::{Option, Some, None};
7374
use uint;
74-
use mem;
7575

7676
/// Conversion from an `Iterator`
7777
pub trait FromIterator<A> {
@@ -2192,6 +2192,27 @@ impl<A: Clone> RandomAccessIterator<A> for Repeat<A> {
21922192
fn idx(&mut self, _: uint) -> Option<A> { Some(self.element.clone()) }
21932193
}
21942194

2195+
type IterateState<'a, T> = (|T|: 'a -> T, Option<T>, bool);
2196+
2197+
/// An iterator that repeatedly applies a given function, starting
2198+
/// from a given seed value.
2199+
pub type Iterate<'a, T> = Unfold<'a, T, IterateState<'a, T>>;
2200+
2201+
/// Creates a new iterator that produces an infinite sequence of
2202+
/// repeated applications of the given function `f`.
2203+
#[allow(visible_private_types)]
2204+
pub fn iterate<'a, T: Clone>(f: |T|: 'a -> T, seed: T) -> Iterate<'a, T> {
2205+
Unfold::new((f, Some(seed), true), |st| {
2206+
let &(ref mut f, ref mut val, ref mut first) = st;
2207+
if *first {
2208+
*first = false;
2209+
} else {
2210+
val.mutate(|x| (*f)(x));
2211+
}
2212+
val.clone()
2213+
})
2214+
}
2215+
21952216
/// Functions for lexicographical ordering of sequences.
21962217
///
21972218
/// Lexicographical ordering through `<`, `<=`, `>=`, `>` requires

branches/auto/src/libcoretest/iter.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,3 +833,12 @@ fn test_min_max_result() {
833833
let r = MinMax(1i,2);
834834
assert_eq!(r.into_option(), Some((1,2)));
835835
}
836+
837+
#[test]
838+
fn test_iterate() {
839+
let mut it = iterate(|x| x * 2, 1u);
840+
assert_eq!(it.next(), Some(1u));
841+
assert_eq!(it.next(), Some(2u));
842+
assert_eq!(it.next(), Some(4u));
843+
assert_eq!(it.next(), Some(8u));
844+
}

branches/auto/src/libstd/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
//! passing. [`sync`](sync/index.html) contains further, primitive, shared
7979
//! memory types, including [`atomics`](sync/atomics/index.html).
8080
//!
81-
//! Common types of I/O, including files, TCP, UDP, pipes, Unix domain sockets,
81+
//! Common types of I/O, including files, TCP, UPD, pipes, Unix domain sockets,
8282
//! timers, and process spawning, are defined in the [`io`](io/index.html) module.
8383
//!
8484
//! Rust's I/O and concurrency depends on a small runtime interface

0 commit comments

Comments
 (0)