Add a repeat function to the prelude
Implements a part of RFC 235. [breaking-change]
This commit is contained in:
@@ -2155,7 +2155,7 @@ type IterateState<'a, T> = (|T|: 'a -> T, Option<T>, bool);
|
|||||||
/// from a given seed value.
|
/// from a given seed value.
|
||||||
pub type Iterate<'a, T> = Unfold<'a, T, IterateState<'a, T>>;
|
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`.
|
/// repeated applications of the given function `f`.
|
||||||
pub fn iterate<'a, T: Clone>(seed: T, f: |T|: 'a -> T) -> Iterate<'a, T> {
|
pub fn iterate<'a, T: Clone>(seed: T, f: |T|: 'a -> T) -> Iterate<'a, T> {
|
||||||
Unfold::new((f, Some(seed), true), |st| {
|
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.
|
/// Functions for lexicographical ordering of sequences.
|
||||||
///
|
///
|
||||||
/// Lexicographical ordering through `<`, `<=`, `>=`, `>` requires
|
/// Lexicographical ordering through `<`, `<=`, `>=`, `>` requires
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ pub use option::{Option, Some, None};
|
|||||||
pub use result::{Result, Ok, Err};
|
pub use result::{Result, Ok, Err};
|
||||||
|
|
||||||
// Reexported functions
|
// Reexported functions
|
||||||
pub use iter::range;
|
pub use iter::{range, repeat};
|
||||||
pub use mem::drop;
|
pub use mem::drop;
|
||||||
|
|
||||||
// Reexported types and traits
|
// Reexported types and traits
|
||||||
|
|||||||
@@ -846,6 +846,14 @@ fn test_iterate() {
|
|||||||
assert_eq!(it.next(), Some(8u));
|
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]
|
#[bench]
|
||||||
fn bench_rposition(b: &mut Bencher) {
|
fn bench_rposition(b: &mut Bencher) {
|
||||||
let it: Vec<uint> = range(0u, 300).collect();
|
let it: Vec<uint> = range(0u, 300).collect();
|
||||||
|
|||||||
@@ -53,7 +53,7 @@
|
|||||||
|
|
||||||
// Reexported functions
|
// Reexported functions
|
||||||
#[doc(no_inline)] pub use from_str::from_str;
|
#[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;
|
#[doc(no_inline)] pub use mem::drop;
|
||||||
|
|
||||||
// Reexported types and traits
|
// Reexported types and traits
|
||||||
|
|||||||
Reference in New Issue
Block a user