about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-11-03 14:17:26 +0000
committerbors <bors@rust-lang.org>2014-11-03 14:17:26 +0000
commit2790505c19b158a5494139dba084b6af82810b96 (patch)
treee4f0b8cd6a79bb657ad12ecf11fedc82f44b0951
parentb9b396cd7506c2e2aa6737adfa80f3404ed81b9d (diff)
parent696f72e84e8420933270be5cde3735b372cee8c5 (diff)
downloadrust-2790505c19b158a5494139dba084b6af82810b96.tar.gz
rust-2790505c19b158a5494139dba084b6af82810b96.zip
auto merge of #18468 : jakub-/rust/iter-repeat, r=alexcrichton
Implements a part of RFC 235.

[breaking-change]
-rw-r--r--src/libcore/iter.rs7
-rw-r--r--src/libcore/prelude.rs2
-rw-r--r--src/libcoretest/iter.rs8
-rw-r--r--src/libstd/prelude.rs2
4 files changed, 16 insertions, 3 deletions
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs
index e2a4fdfe79b..39b319e6ac8 100644
--- a/src/libcore/iter.rs
+++ b/src/libcore/iter.rs
@@ -2155,7 +2155,7 @@ type IterateState<'a, T> = (|T|: 'a -> T, Option<T>, bool);
 /// from a given seed value.
 pub type Iterate<'a, T> = Unfold<'a, T, IterateState<'a, T>>;
 
-/// Creates a new iterator that produces an infinite sequence of
+/// Create a new iterator that produces an infinite sequence of
 /// repeated applications of the given function `f`.
 pub fn iterate<'a, T: Clone>(seed: T, f: |T|: 'a -> T) -> Iterate<'a, T> {
     Unfold::new((f, Some(seed), true), |st| {
@@ -2174,6 +2174,11 @@ pub fn iterate<'a, T: Clone>(seed: T, f: |T|: 'a -> T) -> Iterate<'a, T> {
     })
 }
 
+/// Create a new iterator that endlessly repeats the element `elt`.
+pub fn repeat<T: Clone>(elt: T) -> Repeat<T> {
+    Repeat::new(elt)
+}
+
 /// Functions for lexicographical ordering of sequences.
 ///
 /// Lexicographical ordering through `<`, `<=`, `>=`, `>` requires
diff --git a/src/libcore/prelude.rs b/src/libcore/prelude.rs
index 0d4979a3575..4b08d4f3391 100644
--- a/src/libcore/prelude.rs
+++ b/src/libcore/prelude.rs
@@ -39,7 +39,7 @@ pub use ops::{Slice, SliceMut};
 pub use ops::{Fn, FnMut, FnOnce};
 
 // Reexported functions
-pub use iter::range;
+pub use iter::{range, repeat};
 pub use mem::drop;
 
 // Reexported types and traits
diff --git a/src/libcoretest/iter.rs b/src/libcoretest/iter.rs
index 98db377b0d5..5d333d48e96 100644
--- a/src/libcoretest/iter.rs
+++ b/src/libcoretest/iter.rs
@@ -846,6 +846,14 @@ fn test_iterate() {
     assert_eq!(it.next(), Some(8u));
 }
 
+#[test]
+fn test_repeat() {
+    let mut it = repeat(42u);
+    assert_eq!(it.next(), Some(42u));
+    assert_eq!(it.next(), Some(42u));
+    assert_eq!(it.next(), Some(42u));
+}
+
 #[bench]
 fn bench_rposition(b: &mut Bencher) {
     let it: Vec<uint> = range(0u, 300).collect();
diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs
index 47befb2286b..4dcdde6726a 100644
--- a/src/libstd/prelude.rs
+++ b/src/libstd/prelude.rs
@@ -51,7 +51,7 @@
 
 // Reexported functions
 #[doc(no_inline)] pub use from_str::from_str;
-#[doc(no_inline)] pub use iter::range;
+#[doc(no_inline)] pub use iter::{range, repeat};
 #[doc(no_inline)] pub use mem::drop;
 
 // Reexported types and traits