Skip to content

Commit 696f72e

Browse files
author
Jakub Bukaj
committed
Add a repeat function to the prelude
Implements a part of RFC 235. [breaking-change]
1 parent 52c3fe9 commit 696f72e

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

src/libcore/iter.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -2155,7 +2155,7 @@ type IterateState<'a, T> = (|T|: 'a -> T, Option<T>, bool);
21552155
/// from a given seed value.
21562156
pub type Iterate<'a, T> = Unfold<'a, T, IterateState<'a, T>>;
21572157

2158-
/// Creates a new iterator that produces an infinite sequence of
2158+
/// Create a new iterator that produces an infinite sequence of
21592159
/// repeated applications of the given function `f`.
21602160
pub fn iterate<'a, T: Clone>(seed: T, f: |T|: 'a -> T) -> Iterate<'a, T> {
21612161
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> {
21742174
})
21752175
}
21762176

2177+
/// Create a new iterator that endlessly repeats the element `elt`.
2178+
pub fn repeat<T: Clone>(elt: T) -> Repeat<T> {
2179+
Repeat::new(elt)
2180+
}
2181+
21772182
/// Functions for lexicographical ordering of sequences.
21782183
///
21792184
/// Lexicographical ordering through `<`, `<=`, `>=`, `>` requires

src/libcore/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub use option::{Option, Some, None};
4141
pub use result::{Result, Ok, Err};
4242

4343
// Reexported functions
44-
pub use iter::range;
44+
pub use iter::{range, repeat};
4545
pub use mem::drop;
4646

4747
// Reexported types and traits

src/libcoretest/iter.rs

+8
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,14 @@ fn test_iterate() {
846846
assert_eq!(it.next(), Some(8u));
847847
}
848848

849+
#[test]
850+
fn test_repeat() {
851+
let mut it = repeat(42u);
852+
assert_eq!(it.next(), Some(42u));
853+
assert_eq!(it.next(), Some(42u));
854+
assert_eq!(it.next(), Some(42u));
855+
}
856+
849857
#[bench]
850858
fn bench_rposition(b: &mut Bencher) {
851859
let it: Vec<uint> = range(0u, 300).collect();

src/libstd/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
// Reexported functions
5555
#[doc(no_inline)] pub use from_str::from_str;
56-
#[doc(no_inline)] pub use iter::range;
56+
#[doc(no_inline)] pub use iter::{range, repeat};
5757
#[doc(no_inline)] pub use mem::drop;
5858

5959
// Reexported types and traits

0 commit comments

Comments
 (0)