diff options
| author | Bruno de Oliveira Abinader <bruno.d@partner.samsung.com> | 2014-02-24 22:34:59 -0400 |
|---|---|---|
| committer | Bruno de Oliveira Abinader <bruno.d@partner.samsung.com> | 2014-02-27 08:35:45 -0400 |
| commit | a09a4b882d415ea764f58816b963de0203c4e9f0 (patch) | |
| tree | cd54971b9542d1db381cc735e26d1495033e92d2 /src/libcollections | |
| parent | 43bc6fcc62e3ad92c76ea8f160a6189ac86618f2 (diff) | |
| download | rust-a09a4b882d415ea764f58816b963de0203c4e9f0.tar.gz rust-a09a4b882d415ea764f58816b963de0203c4e9f0.zip | |
Removed list::foldl() in favor of iter().fold()
Diffstat (limited to 'src/libcollections')
| -rw-r--r-- | src/libcollections/list.rs | 43 |
1 files changed, 10 insertions, 33 deletions
diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index d4685f37a39..ce6084923f9 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -54,25 +54,6 @@ impl<T> List<T> { } /** - * 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<T:Clone,U>(z: T, list: @List<U>, 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 * * Apply function `f` to each element of `list`, starting from the first. @@ -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::<int>; - 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::<uint>; + 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] |
