From a09a4b882d415ea764f58816b963de0203c4e9f0 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 22:34:59 -0400 Subject: [PATCH] Removed list::foldl() in favor of iter().fold() --- src/libcollections/list.rs | 43 +++++++++----------------------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index d4685f37a39d..ce6084923f91 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -53,25 +53,6 @@ impl List { } } -/** - * Left fold - * - * Applies `f` to `u` and the first element in the list, then applies `f` to - * the result of the previous call and the second element, and so on, - * returning the accumulated result. - * - * # Arguments - * - * * list - The list to fold - * * z - The initial value - * * f - The function to apply - */ -pub fn foldl(z: T, list: @List, f: |&T, &U| -> T) -> T { - let mut accum: T = z; - iter(list, |element| accum = f(&accum, element)); - accum -} - /** * Search for an element that matches a given predicate * @@ -258,21 +239,17 @@ mod tests { } #[test] - fn test_foldl() { - fn add(a: &uint, b: &int) -> uint { return *a + (*b as uint); } - let list = @List::from_vec([0, 1, 2, 3, 4]); - let empty = @list::Nil::; - assert_eq!(list::foldl(0u, list, add), 10u); - assert_eq!(list::foldl(0u, empty, add), 0u); - } + fn test_fold() { + fn add_(a: uint, b: &uint) -> uint { a + *b } + fn subtract_(a: uint, b: &uint) -> uint { a - *b } - #[test] - fn test_foldl2() { - fn sub(a: &int, b: &int) -> int { - *a - *b - } - let list = @List::from_vec([1, 2, 3, 4]); - assert_eq!(list::foldl(0, list, sub), -10); + let empty = Nil::; + assert_eq!(empty.iter().fold(0u, add_), 0u); + assert_eq!(empty.iter().fold(10u, subtract_), 10u); + + let list = List::from_vec([0u, 1u, 2u, 3u, 4u]); + assert_eq!(list.iter().fold(0u, add_), 10u); + assert_eq!(list.iter().fold(10u, subtract_), 0u); } #[test]