Skip to content

Commit

Permalink
Add a repeat function to the prelude
Browse files Browse the repository at this point in the history
Implements a part of RFC 235.

[breaking-change]
  • Loading branch information
Jakub Bukaj committed Oct 30, 2014
1 parent 52c3fe9 commit 696f72e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/libcore/iter.rs
Expand Up @@ -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| {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/prelude.rs
Expand Up @@ -41,7 +41,7 @@ pub use option::{Option, Some, None};
pub use result::{Result, Ok, Err};

// Reexported functions
pub use iter::range;
pub use iter::{range, repeat};
pub use mem::drop;

// Reexported types and traits
Expand Down
8 changes: 8 additions & 0 deletions src/libcoretest/iter.rs
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/prelude.rs
Expand Up @@ -53,7 +53,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
Expand Down

21 comments on commit 696f72e

@bors
Copy link
Contributor

@bors bors commented on 696f72e Oct 31, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 696f72e Oct 31, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jakub-/rust/iter-repeat = 696f72e into auto

@bors
Copy link
Contributor

@bors bors commented on 696f72e Oct 31, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jakub-/rust/iter-repeat = 696f72e merged ok, testing candidate = 516e7526

@bors
Copy link
Contributor

@bors bors commented on 696f72e Oct 31, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jakub-/rust/iter-repeat = 696f72e into auto

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jakub-/rust/iter-repeat = 696f72e merged ok, testing candidate = 2a425fec

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jakub-/rust/iter-repeat = 696f72e into auto

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jakub-/rust/iter-repeat = 696f72e merged ok, testing candidate = ad7e7cb

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jakub-/rust/iter-repeat = 696f72e into auto

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jakub-/rust/iter-repeat = 696f72e merged ok, testing candidate = c144905a

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jakub-/rust/iter-repeat = 696f72e into auto

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jakub-/rust/iter-repeat = 696f72e merged ok, testing candidate = 2790505

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 696f72e Nov 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 2790505

Please sign in to comment.