Browse files

Merge pull request #1 from pwoolcoc/master

Add repeat function
  • Loading branch information...
2 parents 5439e21 + b0e960a commit 0378cedcb3541c862dca2da94760d396a0707564 @nikomatsakis committed Jan 30, 2012
Showing with 24 additions and 1 deletion.
  1. +24 −1 iter.rs
View
25 iter.rs
@@ -64,6 +64,15 @@ fn to_list<A:copy,IA:iterable<A>>(self: IA) -> [A] {
foldl::<A,[A],IA>(self, [], {|r, a| r + [a]})
}
+fn repeat(times: uint, blk: fn()) {
+ let i = 0u;
+ while i < times {
+ blk();
+ i += 1u;
+ }
+}
+
+
#[test]
fn test_enumerate() {
enumerate(bind vec::iter([0u, 1u, 2u], _)) {|i,j|
@@ -105,4 +114,18 @@ fn test_flat_map_with_list() {
let c = to_list(b);
#debug["c = %?", c];
assert c == [1, 2, 2, 3, 3, 3];
-}
+}
+
+#[test]
+fn test_repeat() {
+ let c = [],
+ i = 0u;
+ repeat(5u) {||
+ c += [(i * i)];
+ i += 1u;
+ };
+ #debug["c = %?", c];
+ assert c == [0u, 1u, 4u, 9u, 16u];
+}
+
+

0 comments on commit 0378ced

Please sign in to comment.