Skip to content

Commit a4b3c99

Browse files
author
Kingsley Hendrickse
committed
fix append so it does not mutate
1 parent 0bb28c3 commit a4b3c99

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

js/array_extra.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
function concat(a, b) {
2-
return a.reduceRight(function (coll, item) {
3-
coll.unshift(item);
4-
return coll;
5-
}, b);
6-
}
7-
81
function groupsOf(arr, size) {
92
var arrMulti = [];
103
var groups = Math.ceil(arr.length/size);

source/ArrayExtra.mint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Array.Extra {
1515
Array.Extra.append([1,1,2] [3,5,8]) == [1,1,2,3,5,8]
1616
*/
1717
fun append (array1 : Array(a), array2 : Array(a)) : Array(a) {
18-
`concat(array1, array2)`
18+
`array1.concat(array2)`
1919
}
2020

2121
/*

tests/ArrayExtra.mint

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,43 @@ suite "Array groupsOf" {
192192
}
193193
}
194194
}
195+
196+
suite "Array functions should not mutate existing arrays" {
197+
test "append should not mutate" {
198+
try {
199+
a =
200+
[
201+
1,
202+
2,
203+
3
204+
]
205+
206+
b =
207+
[
208+
4,
209+
5,
210+
6
211+
]
212+
213+
c =
214+
Array.Extra.append(a, b)
215+
216+
(a == [
217+
1,
218+
2,
219+
3
220+
] && b == [
221+
4,
222+
5,
223+
6
224+
] && c == [
225+
1,
226+
2,
227+
3,
228+
4,
229+
5,
230+
6
231+
])
232+
}
233+
}
234+
}

0 commit comments

Comments
 (0)