Skip to content

Commit 4d086ce

Browse files
committed
Add immutable sort to Array
1 parent b5e3e8c commit 4d086ce

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/Core__Array.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ function lastIndexOfOpt(arr, item) {
2121

2222
}
2323

24+
function sort(arr, cmp) {
25+
var result = arr.slice();
26+
result.sort(cmp);
27+
return result;
28+
}
29+
2430
function reduce(a, x, f) {
2531
var f$1 = Curry.__2(f);
2632
var r = x;
@@ -107,6 +113,7 @@ function flatMap(a, f) {
107113
}
108114

109115
export {
116+
sort ,
110117
indexOfOpt ,
111118
lastIndexOfOpt ,
112119
reduce ,

src/Core__Array.res

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<
3939

4040
@send external shift: array<'a> => option<'a> = "shift"
4141

42-
@send external sortInPlace: (array<'a>, ('a, 'a) => int) => unit = "sort"
43-
4442
@variadic @send
4543
external spliceInPlace: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit =
4644
"splice"
@@ -78,6 +76,14 @@ let lastIndexOfOpt = (arr, item) =>
7876
@send external sliceToEnd: (array<'a>, ~start: int) => array<'a> = "slice"
7977
@send external copy: array<'a> => array<'a> = "slice"
8078

79+
@send external sortInPlace: (array<'a>, ('a, 'a) => int) => unit = "sort"
80+
81+
let sort = (arr, cmp) => {
82+
let result = copy(arr)
83+
sortInPlace(result, cmp)
84+
result
85+
}
86+
8187
@send external toString: array<'a> => string = "toString"
8288
@send external toLocaleString: array<'a> => string = "toLocaleString"
8389

src/Core__Array.resi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<
2020
@variadic @send external pushMany: (array<'a>, array<'a>) => unit = "push"
2121
@send external reverseInPlace: array<'a> => unit = "reverse"
2222
@send external shift: array<'a> => option<'a> = "shift"
23+
let sort: (array<'a>, ('a, 'a) => int) => array<'a>
2324
@send external sortInPlace: (array<'a>, ('a, 'a) => int) => unit = "sort"
2425
@variadic @send
2526
external spliceInPlace: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit =

0 commit comments

Comments
 (0)