on master help sign in
Resolve issue with getting negative indices
3bf9712Evan Czaplicki
libraries/Array.elm+5−4
@@ -123,12 +123,13 @@ get = Native.Array.get
123
123
124{-| Return Just the element at the index or Nothing if the index is out of range.
124{-| Return Just the element at the index or Nothing if the index is out of range.
125
125
126− getMaybe 2 (fromList [3,2,1]) == Just 2
126+ getMaybe 2 (fromList [3,2,1]) == Just 2
127− getMaybe 5 (fromList [3,2,1]) == Nothing
127+ getMaybe 5 (fromList [3,2,1]) == Nothing
128+ getMaybe -1 (fromList [3,2,1]) == Nothing
128-}
129-}
129getMaybe : Int -> Array a -> Maybe a
130getMaybe : Int -> Array a -> Maybe a
130getMaybe i array =
131getMaybe i array =
131− if Native.Array.length array > i
132+ if 0 <= i && i < Native.Array.length array
132 then Just (Native.Array.get i array)
133 then Just (Native.Array.get i array)
133 else Nothing
134 else Nothing
134
135
@@ -140,7 +141,7 @@ element is returned.
140-}
141-}
141getWithDefault : a -> Int -> Array a -> a
142getWithDefault : a -> Int -> Array a -> a
142getWithDefault default i array =
143getWithDefault default i array =
143− if Native.Array.length array > i
144+ if 0 <= i && i < Native.Array.length array
144 then Native.Array.get i array
145 then Native.Array.get i array
145 else default
146 else default
146
147
libraries/Native/Array.js+18−15
@@ -25,18 +25,21 @@ Elm.Native.Array.make = function(elm) {
25 var empty = { ctor:"_Array", height:0, table:new Array() };
25 var empty = { ctor:"_Array", height:0, table:new Array() };
26
26
27 // Gets the value at index i recursively.
27 // Gets the value at index i recursively.
28− function get(i, a) {
28+ function get(i, array) {
29− if (a.height == 0) {
30− if (i < a.table.length) {
29+ if (i < 0 || i >= length(array)) {
31− return a.table[i];
32− } else {
33− throw new Error("Index "+ i +" is out of range. Check the length of your array first or use getSafe or getMaybe.");
30+ throw new Error("Index " + i + " is out of range. Check the length of " +
31+ "your array first or use getMaybe or getWithDefault.");
34 }
32 }
33+ return unsafeGet(i, array);
35− }
34+ }
36
35
36+ function unsafeGet(i, array) {
37+ if (array.height == 0) {
38+ return array.table[i];
39+ }
37− var slot = getSlot(i, a);
40+ var slot = getSlot(i, array);
38− var sub = slot > 0 ? a.lengths[slot-1] : 0;
41+ var offset = slot > 0 ? array.lengths[slot-1] : 0;
39− return get(i - sub, a.table[slot]);
42+ return unsafeGet(i - offset, array.table[slot]);
40 }
43 }
41
44
42 // Sets the value at the index i. Only the nodes leading to i will get
45 // Sets the value at the index i. Only the nodes leading to i will get
@@ -65,7 +68,7 @@ Elm.Native.Array.make = function(elm) {
65 return pushed;
68 return pushed;
66 }
69 }
67
70
68− newTree = create(item, a.height);
71+ var newTree = create(item, a.height);
69 return siblise(a, newTree);
72 return siblise(a, newTree);
70 }
73 }
71
74
@@ -423,11 +426,11 @@ Elm.Native.Array.make = function(elm) {
423 }
426 }
424
427
425 // Returns how many items are in the tree.
428 // Returns how many items are in the tree.
426− function length(a) {
429+ function length(array) {
427− if (a.height == 0) {
430+ if (array.height == 0) {
428− return a.table.length;
431+ return array.table.length;
429 } else {
432 } else {
430− return a.lengths[a.lengths.length - 1];
433+ return array.lengths[array.lengths.length - 1];
431 }
434 }
432 }
435 }
433
436