Skip to content

Commit bea4804

Browse files
committed
---
yaml --- r: 6022 b: refs/heads/master c: 39b729e h: refs/heads/master v: v3
1 parent 39035f6 commit bea4804

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
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: 49e8ffa34f3d66410e733e873894ddf366d23d2d
2+
refs/heads/master: 39b729e36f813a58de6b1f5641c94f47b7a6c277

trunk/src/lib/list.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ Function: foldl
3434
3535
Left fold
3636
37-
Applies `f` to the first argument in the list and `u`, then applies
38-
`f` to the second argument and the result of the previous call,
37+
Applies `f` to `u` and the first element in the list, then applies
38+
`f` to the result of the previous call and the second element,
3939
and so on, returning the accumulated result.
4040
4141
Parameters:
4242
4343
ls - The list to fold
44-
u - The initial value
44+
z - The initial value
4545
f - The function to apply
4646
*/
47-
fn foldl<T, U>(ls: list<T>, u: U, f: block(T, U) -> U) -> U {
48-
let accum: U = u;
47+
fn foldl<T, U>(ls: list<U>, z: T, f: block(T, U) -> T) -> T {
48+
let accum: T = z;
4949
let ls = ls;
5050
while true {
5151
alt ls {
52-
cons(hd, tl) { accum = f(hd, accum); ls = *tl; }
52+
cons(hd, tl) { accum = f(accum, hd); ls = *tl; }
5353
nil. { break; }
5454
}
5555
}
@@ -100,7 +100,7 @@ Function: len
100100
Returns the length of a list
101101
*/
102102
fn len<T>(ls: list<T>) -> uint {
103-
fn count<T>(_t: T, &&u: uint) -> uint { ret u + 1u; }
103+
fn count<T>(&&u: uint, _t: T) -> uint { ret u + 1u; }
104104
ret foldl(ls, 0u, bind count(_, _));
105105
}
106106

trunk/src/test/stdtest/list.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,21 @@ fn test_from_vec_mut() {
2525
#[test]
2626
fn test_foldl() {
2727
let l = from_vec([0, 1, 2, 3, 4]);
28-
fn add(&&a: int, &&b: uint) -> uint { ret (a as uint) + b; }
28+
fn add(&&a: uint, &&b: int) -> uint { ret a + (b as uint); }
2929
let rs = list::foldl(l, 0u, add);
3030
assert (rs == 10u);
3131
}
3232

33+
#[test]
34+
fn test_foldl2() {
35+
fn sub(&&a: int, &&b: int) -> int {
36+
a - b
37+
}
38+
let l = from_vec([1, 2, 3, 4]);
39+
let sum = list::foldl(l, 0, sub);
40+
assert sum == -10;
41+
}
42+
3343
#[test]
3444
fn test_find_success() {
3545
let l = from_vec([0, 1, 2]);

0 commit comments

Comments
 (0)