Skip to content

Commit b45bfe8

Browse files
committed
[Belt] Add List.sort
Signed-off-by: Zach Ploskey <zach@ploskey.com>
1 parent 234bc0f commit b45bfe8

File tree

5 files changed

+98
-11
lines changed

5 files changed

+98
-11
lines changed

Diff for: jscomp/others/belt_List.ml

+7
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,13 @@ let setAssocU xs x k eq =
692692

693693
let setAssoc xs x k eq = setAssocU xs x k (fun [@bs] a b -> eq a b)
694694

695+
let sort xs cmp =
696+
let arr = toArray xs in
697+
begin
698+
Belt_SortArray.stableSortInPlaceBy arr cmp;
699+
fromArray arr;
700+
end
701+
695702
let rec getByU xs p =
696703
match xs with
697704
| [] -> None

Diff for: jscomp/others/belt_List.mli

+8-4
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,12 @@ val setAssoc: ('a * 'c) t -> 'a -> 'c -> ('a -> 'a -> bool) -> ('a * 'c) t
528528
setAssoc [1,"a"; 3, "c"] 2 "2" (=) =
529529
[2,"2"; 1,"a"; 3, "c"]
530530
]}
531-
*)
532-
533-
534-
531+
*)
535532

533+
val sort: 'a t -> ('a -> 'a -> int) -> 'a t
534+
(** [sort xs]
535+
Returns a sorted list.
536+
@example {[
537+
sort (fun a b -> a - b) [5; 4; 9; 3; 7] = [3; 4; 5; 7; 9]
538+
]}
539+
*)

Diff for: jscomp/test/bs_list_test.js

+69-7
Original file line numberDiff line numberDiff line change
@@ -2181,7 +2181,69 @@ makeTest(2);
21812181

21822182
makeTest(3);
21832183

2184-
b("File \"bs_list_test.ml\", line 320, characters 4-11", 1 - Belt_List.eq(/* :: */[
2184+
function cmp(a, b) {
2185+
return a - b | 0;
2186+
}
2187+
2188+
eq("SORT", Belt_List.sort(/* :: */[
2189+
5,
2190+
/* :: */[
2191+
4,
2192+
/* :: */[
2193+
3,
2194+
/* :: */[
2195+
2,
2196+
/* [] */0
2197+
]
2198+
]
2199+
]
2200+
], cmp), /* :: */[
2201+
2,
2202+
/* :: */[
2203+
3,
2204+
/* :: */[
2205+
4,
2206+
/* :: */[
2207+
5,
2208+
/* [] */0
2209+
]
2210+
]
2211+
]
2212+
]);
2213+
2214+
eq("SORT", Belt_List.sort(/* :: */[
2215+
3,
2216+
/* :: */[
2217+
9,
2218+
/* :: */[
2219+
37,
2220+
/* :: */[
2221+
3,
2222+
/* :: */[
2223+
1,
2224+
/* [] */0
2225+
]
2226+
]
2227+
]
2228+
]
2229+
], cmp), /* :: */[
2230+
1,
2231+
/* :: */[
2232+
3,
2233+
/* :: */[
2234+
3,
2235+
/* :: */[
2236+
9,
2237+
/* :: */[
2238+
37,
2239+
/* [] */0
2240+
]
2241+
]
2242+
]
2243+
]
2244+
]);
2245+
2246+
b("File \"bs_list_test.ml\", line 326, characters 4-11", 1 - Belt_List.eq(/* :: */[
21852247
1,
21862248
/* :: */[
21872249
2,
@@ -2200,7 +2262,7 @@ b("File \"bs_list_test.ml\", line 320, characters 4-11", 1 - Belt_List.eq(/* ::
22002262
return +(x === y);
22012263
})));
22022264

2203-
b("File \"bs_list_test.ml\", line 321, characters 4-11", Belt_List.eq(/* :: */[
2265+
b("File \"bs_list_test.ml\", line 327, characters 4-11", Belt_List.eq(/* :: */[
22042266
1,
22052267
/* :: */[
22062268
2,
@@ -2222,7 +2284,7 @@ b("File \"bs_list_test.ml\", line 321, characters 4-11", Belt_List.eq(/* :: */[
22222284
return +(x === y);
22232285
})));
22242286

2225-
b("File \"bs_list_test.ml\", line 322, characters 4-11", 1 - Belt_List.eq(/* :: */[
2287+
b("File \"bs_list_test.ml\", line 328, characters 4-11", 1 - Belt_List.eq(/* :: */[
22262288
1,
22272289
/* :: */[
22282290
2,
@@ -2244,7 +2306,7 @@ b("File \"bs_list_test.ml\", line 322, characters 4-11", 1 - Belt_List.eq(/* ::
22442306
return +(x === y);
22452307
})));
22462308

2247-
b("File \"bs_list_test.ml\", line 323, characters 4-11", 1 - Belt_List.eq(/* :: */[
2309+
b("File \"bs_list_test.ml\", line 329, characters 4-11", 1 - Belt_List.eq(/* :: */[
22482310
1,
22492311
/* :: */[
22502312
2,
@@ -2279,7 +2341,7 @@ var u1 = Belt_List.keepMap(u0, (function (x) {
22792341
}
22802342
}));
22812343

2282-
eq("File \"bs_list_test.ml\", line 327, characters 5-12", u1, /* :: */[
2344+
eq("File \"bs_list_test.ml\", line 333, characters 5-12", u1, /* :: */[
22832345
1,
22842346
/* :: */[
22852347
8,
@@ -2290,7 +2352,7 @@ eq("File \"bs_list_test.ml\", line 327, characters 5-12", u1, /* :: */[
22902352
]
22912353
]);
22922354

2293-
b("File \"bs_list_test.ml\", line 328, characters 4-11", Caml_obj.caml_equal(Belt_List.keepMap(/* :: */[
2355+
b("File \"bs_list_test.ml\", line 334, characters 4-11", Caml_obj.caml_equal(Belt_List.keepMap(/* :: */[
22942356
1,
22952357
/* :: */[
22962358
2,
@@ -2316,7 +2378,7 @@ b("File \"bs_list_test.ml\", line 328, characters 4-11", Caml_obj.caml_equal(Bel
23162378
]
23172379
]));
23182380

2319-
b("File \"bs_list_test.ml\", line 332, characters 4-11", +(Belt_List.keepMap(/* :: */[
2381+
b("File \"bs_list_test.ml\", line 338, characters 4-11", +(Belt_List.keepMap(/* :: */[
23202382
1,
23212383
/* :: */[
23222384
2,

Diff for: jscomp/test/bs_list_test.ml

+6
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ let () =
316316
makeTest 2;
317317
makeTest 3
318318

319+
let () =
320+
let (=~) = eq "SORT" in
321+
let cmp a b = a - b in
322+
N.sort [5; 4; 3; 2] cmp =~ [2; 3; 4; 5];
323+
N.sort [3; 9; 37; 3; 1] cmp =~ [1; 3; 3; 9; 37]
324+
319325
let () =
320326
b __LOC__ (not @@ N.eq [1;2;3] [1;2] (fun x y -> x = y));
321327
b __LOC__ (N.eq [1;2;3] [1;2;3] (fun x y -> x = y));

Diff for: lib/js/belt_List.js

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var Curry = require("./curry.js");
44
var Belt_Array = require("./belt_Array.js");
5+
var Belt_SortArray = require("./belt_SortArray.js");
56

67
function head(x) {
78
if (x) {
@@ -1209,6 +1210,12 @@ function setAssoc(xs, x, k, eq) {
12091210
return setAssocU(xs, x, k, Curry.__2(eq));
12101211
}
12111212

1213+
function sort(xs, cmp) {
1214+
var arr = toArray(xs);
1215+
Belt_SortArray.stableSortInPlaceBy(arr, cmp);
1216+
return fromArray(arr);
1217+
}
1218+
12121219
function getByU(_xs, p) {
12131220
while(true) {
12141221
var xs = _xs;
@@ -1445,4 +1452,5 @@ exports.removeAssocU = removeAssocU;
14451452
exports.removeAssoc = removeAssoc;
14461453
exports.setAssocU = setAssocU;
14471454
exports.setAssoc = setAssoc;
1455+
exports.sort = sort;
14481456
/* No side effect */

0 commit comments

Comments
 (0)