Skip to content

Commit bc3f09f

Browse files
committed
---
yaml --- r: 5999 b: refs/heads/master c: d96c419 h: refs/heads/master i: 5997: bc05364 5995: f706af7 5991: 8627909 5983: a0cc07d v: v3
1 parent a4c8cb3 commit bc3f09f

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 8ad857f45301b88537411cc2c9dd068c970172c7
2+
refs/heads/master: d96c419b14e21b5a1444ea6122659bc8966a8b1c

trunk/src/lib/vec.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,27 @@ Function: foldl
435435
436436
Reduce a vector from left to right
437437
*/
438-
fn foldl<T, U>(p: block(U, T) -> U, z: U, v: [mutable? T]) -> U {
438+
fn foldl<T, U>(p: block(T, U) -> T, z: T, v: [mutable? U]) -> T {
439439
let accum = z;
440440
iter(v) { |elt|
441441
accum = p(accum, elt);
442442
}
443443
ret accum;
444444
}
445445

446+
/*
447+
Function: foldr
448+
449+
Reduce a vector from right to left
450+
*/
451+
fn foldr<T, U>(p: block(T, U) -> U, z: U, v: [mutable? T]) -> U {
452+
let accum = z;
453+
riter(v) { |elt|
454+
accum = p(elt, accum);
455+
}
456+
ret accum;
457+
}
458+
446459
/*
447460
Function: any
448461

trunk/src/test/stdtest/vec.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ fn test_foldl2() {
302302
assert sum == -10;
303303
}
304304

305+
#[test]
306+
fn test_foldr() {
307+
fn sub(&&a: int, &&b: int) -> int {
308+
a - b
309+
}
310+
let v = [1, 2, 3, 4];
311+
let sum = vec::foldr(sub, 0, v);
312+
assert sum == -2;
313+
}
314+
305315
#[test]
306316
fn iter_empty() {
307317
let i = 0;

0 commit comments

Comments
 (0)