on master help sign in
Split set
49b4ca8Christian Widera
libraries/Native/Array.js+8−4
@@ -47,10 +47,14 @@ Elm.Native.Array.make = function(elm) {
47
47
48 // Sets the value at the index i. Only the nodes leading to i will get
48 // Sets the value at the index i. Only the nodes leading to i will get
49 // copied and updated.
49 // copied and updated.
50− function set(i, item, a) {
50+ function set(i, item, array) {
51− if (length(a) <= i) {
51+ if (i < 0 || length(array) <= i) {
52− return a;
52+ return array;
53 }
53 }
54+ return unsafeSet(i, item, array);
55+ }
56+
57+ function unsafeSet(i, item, a) {
54 var newA = nodeCopy(a);
58 var newA = nodeCopy(a);
55
59
56 if (a.height == 0) {
60 if (a.height == 0) {
@@ -58,7 +62,7 @@ Elm.Native.Array.make = function(elm) {
58 } else {
62 } else {
59 var slot = getSlot(i, a);
63 var slot = getSlot(i, a);
60 var sub = slot > 0 ? a.lengths[slot-1] : 0;
64 var sub = slot > 0 ? a.lengths[slot-1] : 0;
61− newA.table[slot] = set(i - sub, item, a.table[slot]);
65+ newA.table[slot] = unsafeSet(i - sub, item, a.table[slot]);
62 }
66 }
63 return newA;
67 return newA;
64 }
68 }