core: Add iter::reverse

This commit is contained in:
Brian Anderson 2012-02-12 02:44:40 -08:00
parent c21db3bbc2
commit 85175d639f

View file

@ -77,6 +77,11 @@ fn to_list<A:copy,IA:iterable<A>>(self: IA) -> [A] {
foldl::<A,[A],IA>(self, [], {|r, a| r + [a]}) foldl::<A,[A],IA>(self, [], {|r, a| r + [a]})
} }
// FIXME: This could be made more efficient with an riterable interface
fn reverse<A:copy,IA:iterable<A>>(self: IA, blk: fn(A)) {
vec::riter(to_list(self), blk)
}
fn repeat(times: uint, blk: fn()) { fn repeat(times: uint, blk: fn()) {
let i = 0u; let i = 0u;
while i < times { while i < times {
@ -214,3 +219,8 @@ fn test_max() {
fn test_max_empty() { fn test_max_empty() {
max::<int, [int]>([]); max::<int, [int]>([]);
} }
#[test]
fn test_reverse() {
assert to_list(bind reverse([1, 2, 3], _)) == [3, 2, 1];
}