Skip to content

Commit 0ed37ec

Browse files
authored
Add (immutable) sort to Array (#24)
* Add immutable sort to Array * Add Array.sortInPlace for List.sort
1 parent b5e3e8c commit 0ed37ec

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
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 =

src/Core__List.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as Curry from "rescript/lib/es6/curry.js";
44
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
55
import * as Caml_option from "rescript/lib/es6/caml_option.js";
66
import * as Core__Array from "./Core__Array.mjs";
7-
import * as Belt_SortArray from "rescript/lib/es6/belt_SortArray.js";
87

98
function head(x) {
109
if (x) {
@@ -1177,9 +1176,8 @@ function setAssoc(xs, x, k, eq) {
11771176
}
11781177

11791178
function sort(xs, cmp) {
1180-
var cmp$1 = Curry.__2(cmp);
11811179
var arr = toArray(xs);
1182-
Belt_SortArray.stableSortInPlaceByU(arr, cmp$1);
1180+
arr.sort(cmp);
11831181
return fromArray(arr);
11841182
}
11851183

src/Core__List.res

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,14 +793,12 @@ let setAssocU = (xs, x, k, eq) =>
793793

794794
let setAssoc = (xs, x, k, eq) => setAssocU(xs, x, k, (. a, b) => eq(a, b))
795795

796-
let sortU = (xs, cmp) => {
796+
let sort = (xs, cmp) => {
797797
let arr = toArray(xs)
798-
Belt_SortArray.stableSortInPlaceByU(arr, cmp)
798+
Core__Array.sortInPlace(arr, cmp)
799799
fromArray(arr)
800800
}
801801

802-
let sort = (xs, cmp) => sortU(xs, (. x, y) => cmp(x, y))
803-
804802
let rec getByU = (xs, p) =>
805803
switch xs {
806804
| list{} => None

0 commit comments

Comments
 (0)