on master help sign in
Merge pull request #650 from Xashili/patch-2

Fix Array.append with an empty array

commit0bc1f4b1fc97fdd16f733b05405924459e1ebba2 authorEvan Czaplicki <info@elm-lang.org> dateMon 9 Jun 2014, 19:11 (12 years ago) parent5d39e64d12a175
2 files changed+7−3
libraries/Native/Array.js+5−3 tests/libraries/Test/Array.elm+2
libraries/Native/Array.js+5−3
@@ -357,9 +357,11 @@ Elm.Native.Array.make = function(elm) {
357357 // TODO: Add support for appending trees of different sizes. Current
358358 // behavior will just rise the lower tree and then append them.
359359 function append(a,b) {
360− if (b.height > a.height) { return append(parentise(a, b.height), b); }
361− if (a.height > b.height) { return append(a, parentise(b, a.height)); }
362− if (a.height == 0) { return append(parentise(a, 1), parentise(b, 1)); }
360+ if (a.table.length == 0) return b;
361+ if (b.table.length == 0) return a;
362+ if (b.height > a.height) return append(parentise(a, b.height), b);
363+ if (a.height > b.height) return append(a, parentise(b, a.height));
364+ if (a.height == 0) return append(parentise(a, 1), parentise(b, 1));
363365
364366 var c = append_(a, b);
365367 if (c[1].table.length > 0) {
tests/libraries/Test/Array.elm+2
@@ -26,6 +26,8 @@ tests =
2626 , test "length - Long" <| assertEqual 10000 (Array.length (Array.repeat 10000 0))
2727 , test "push" <| assertEqual (Array.fromList [1,2,3]) (Array.push 3 (Array.fromList [1,2]))
2828 , test "append" <| assertEqual [42,42,81,81,81] (Array.toList (Array.append (Array.repeat 2 42) (Array.repeat 3 81)))
29+ , test "appendEmpty 1" <| assertEqual [1..33] (Array.toList (Array.append Array.empty (Array.fromList [1..33])))
30+ , test "appendEmpty 2" <| assertEqual [1..33] (Array.toList (Array.append (Array.fromList [1..33]) Array.empty))
2931 ]
3032 getAndSetTests = suite "Get and Set"
3133 [ test "get" <| assertEqual (Just 2) (Array.get 1 (Array.fromList [3,2,1]))