diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 6af10bd8a35..eec41833351 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -73,6 +73,16 @@ fn foldl>(self: IA, b0: B, blk: fn(B, A) -> B) -> B { ret b; } +fn foldr>( + self: IA, b0: B, blk: fn(A, B) -> B) -> B { + + let b = b0; + reverse(self) {|a| + b = blk(a, b); + } + ret b; +} + fn to_list>(self: IA) -> [A] { foldl::(self, [], {|r, a| r + [a]}) } @@ -238,4 +248,13 @@ fn test_reverse() { #[test] fn test_count() { assert count([1, 2, 1, 2, 1], 1) == 3u; +} + +#[test] +fn test_foldr() { + fn sub(&&a: int, &&b: int) -> int { + a - b + } + let sum = foldr([1, 2, 3, 4], 0, sub); + assert sum == -2; } \ No newline at end of file