on master help sign in
Fix bug in which foldl and foldr were swapped!
commit3fb4ea1a1afafb27be615975ad2b49f09723d38f authorEvan Czaplicki <info@elm-lang.org> dateWed 16 Apr 2014, 16:27 (13 years ago) parent1edc02f
3 files changed+6−6
libraries/Array.elm+2−2 libraries/Native/Array.js+2−2 tests/elm/Test/Array.elm+2−2
libraries/Array.elm+2−2
@@ -82,14 +82,14 @@ map = Native.Array.map
8282indexedMap : (Int -> a -> b) -> Array a -> Array b
8383indexedMap = Native.Array.indexedMap
8484
85−{-| Reduce an array from the left.
85+{-| Reduce an array from the left. Read `foldl` as &ldquo;fold from the left&rdquo;.
8686
8787 foldl (::) [] (fromList [1,2,3]) == [3,2,1]
8888-}
8989foldl : (a -> b -> b) -> b -> Array a -> b
9090foldl = Native.Array.foldl
9191
92−{-| Reduce an array from the right.
92+{-| Reduce an array from the right. Read `foldr` as &ldquo;fold from the right&rdquo;.
9393
9494 foldr (+) 0 (repeat 3 5) == 15
9595-}
libraries/Native/Array.js+2−2
@@ -242,14 +242,14 @@ Elm.Native.Array.make = function(elm) {
242242 }
243243
244244 function foldl(f, b, a) {
245− for (var i = a.table.length - 1; i >= 0; i--) {
245+ for (var i = 0; i < a.table.length; i++) {
246246 b = A2(f, a.height == 0 ? a.table[i] : foldl(f, b, a.table[i]), b);
247247 }
248248 return b;
249249 }
250250
251251 function foldr(f, b, a) {
252− for (var i = 0; i < a.table.length; i++) {
252+ for (var i = a.table.length; i--; ) {
253253 b = A2(f, a.height == 0 ? a.table[i] : foldr(f, b, a.table[i]), b);
254254 }
255255 return b;
tests/elm/Test/Array.elm+2−2
@@ -40,8 +40,8 @@ tests =
4040 ]
4141 mappingAndFoldingTests = [ test "map" <| assertEqual (Array.map sqrt (Array.fromList [1,4,9])) (Array.fromList [1,2,3])
4242 , test "indexedMap" <| assertEqual (Array.indexedMap (*) (Array.fromList [5,5,5])) (Array.fromList [0,5,10])
43− , test "foldl" <| assertEqual (Array.foldl (::) [] (Array.fromList [1,2,3])) [1,2,3]
43+ , test "foldl" <| assertEqual (Array.foldl (::) [] (Array.fromList [1,2,3])) [3,2,1]
4444 , test "foldr" <| assertEqual (Array.foldr (+) 0 (Array.repeat 3 5)) 15
45− , test "foldl 2" <| assertEqual (Array.foldr (::) [] (Array.fromList [1,2,3])) [3,2,1]
45+ , test "foldr 2" <| assertEqual (Array.foldr (::) [] (Array.fromList [1,2,3])) [1,2,3]
4646 ]
4747 in List.concat [creationTests, basicsTests, getAndSetTests, takingArraysApartTests, mappingAndFoldingTests]