@@ -53,25 +53,6 @@ impl<T> List<T> {
53
53
}
54
54
}
55
55
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
-
75
56
/**
76
57
* Search for an element that matches a given predicate
77
58
*
@@ -258,21 +239,17 @@ mod tests {
258
239
}
259
240
260
241
#[ 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( 0 u, list, add) , 10 u) ;
266
- assert_eq ! ( list:: foldl( 0 u, empty, add) , 0 u) ;
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 }
268
245
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 ( 0 u , add_ ) , 0 u ) ;
248
+ assert_eq ! ( empty . iter ( ) . fold ( 10 u , subtract_ ) , 10 u ) ;
249
+
250
+ let list = List :: from_vec ( [ 0 u , 1 u , 2 u , 3 u , 4 u ] ) ;
251
+ assert_eq ! ( list. iter ( ) . fold ( 0 u , add_ ) , 10 u ) ;
252
+ assert_eq ! ( list. iter ( ) . fold ( 10 u , subtract_ ) , 0 u ) ;
276
253
}
277
254
278
255
#[ test]
0 commit comments