Skip to content

Commit a09a4b8

Browse files
committed
Removed list::foldl() in favor of iter().fold()
1 parent 43bc6fc commit a09a4b8

File tree

1 file changed

+10
-33
lines changed

1 file changed

+10
-33
lines changed

src/libcollections/list.rs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,6 @@ impl<T> List<T> {
5353
}
5454
}
5555

56-
/**
57-
* Left fold
58-
*
59-
* Applies `f` to `u` and the first element in the list, then applies `f` to
60-
* the result of the previous call and the second element, and so on,
61-
* returning the accumulated result.
62-
*
63-
* # Arguments
64-
*
65-
* * list - The list to fold
66-
* * z - The initial value
67-
* * f - The function to apply
68-
*/
69-
pub fn foldl<T:Clone,U>(z: T, list: @List<U>, f: |&T, &U| -> T) -> T {
70-
let mut accum: T = z;
71-
iter(list, |element| accum = f(&accum, element));
72-
accum
73-
}
74-
7556
/**
7657
* Search for an element that matches a given predicate
7758
*
@@ -258,21 +239,17 @@ mod tests {
258239
}
259240

260241
#[test]
261-
fn test_foldl() {
262-
fn add(a: &uint, b: &int) -> uint { return *a + (*b as uint); }
263-
let list = @List::from_vec([0, 1, 2, 3, 4]);
264-
let empty = @list::Nil::<int>;
265-
assert_eq!(list::foldl(0u, list, add), 10u);
266-
assert_eq!(list::foldl(0u, empty, add), 0u);
267-
}
242+
fn test_fold() {
243+
fn add_(a: uint, b: &uint) -> uint { a + *b }
244+
fn subtract_(a: uint, b: &uint) -> uint { a - *b }
268245

269-
#[test]
270-
fn test_foldl2() {
271-
fn sub(a: &int, b: &int) -> int {
272-
*a - *b
273-
}
274-
let list = @List::from_vec([1, 2, 3, 4]);
275-
assert_eq!(list::foldl(0, list, sub), -10);
246+
let empty = Nil::<uint>;
247+
assert_eq!(empty.iter().fold(0u, add_), 0u);
248+
assert_eq!(empty.iter().fold(10u, subtract_), 10u);
249+
250+
let list = List::from_vec([0u, 1u, 2u, 3u, 4u]);
251+
assert_eq!(list.iter().fold(0u, add_), 10u);
252+
assert_eq!(list.iter().fold(10u, subtract_), 0u);
276253
}
277254

278255
#[test]

0 commit comments

Comments
 (0)