Skip to content

Add (immutable) sort to Array #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Core__Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ function lastIndexOfOpt(arr, item) {

}

function sort(arr, cmp) {
var result = arr.slice();
result.sort(cmp);
return result;
}

function reduce(a, x, f) {
var f$1 = Curry.__2(f);
var r = x;
Expand Down Expand Up @@ -107,6 +113,7 @@ function flatMap(a, f) {
}

export {
sort ,
indexOfOpt ,
lastIndexOfOpt ,
reduce ,
Expand Down
10 changes: 8 additions & 2 deletions src/Core__Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<

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

@send external sortInPlace: (array<'a>, ('a, 'a) => int) => unit = "sort"

@variadic @send
external spliceInPlace: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit =
"splice"
Expand Down Expand Up @@ -78,6 +76,14 @@ let lastIndexOfOpt = (arr, item) =>
@send external sliceToEnd: (array<'a>, ~start: int) => array<'a> = "slice"
@send external copy: array<'a> => array<'a> = "slice"

@send external sortInPlace: (array<'a>, ('a, 'a) => int) => unit = "sort"

let sort = (arr, cmp) => {
let result = copy(arr)
sortInPlace(result, cmp)
result
}

@send external toString: array<'a> => string = "toString"
@send external toLocaleString: array<'a> => string = "toLocaleString"

Expand Down
1 change: 1 addition & 0 deletions src/Core__Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<
@variadic @send external pushMany: (array<'a>, array<'a>) => unit = "push"
@send external reverseInPlace: array<'a> => unit = "reverse"
@send external shift: array<'a> => option<'a> = "shift"
let sort: (array<'a>, ('a, 'a) => int) => array<'a>
@send external sortInPlace: (array<'a>, ('a, 'a) => int) => unit = "sort"
@variadic @send
external spliceInPlace: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit =
Expand Down
4 changes: 1 addition & 3 deletions src/Core__List.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as Curry from "rescript/lib/es6/curry.js";
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
import * as Caml_option from "rescript/lib/es6/caml_option.js";
import * as Core__Array from "./Core__Array.mjs";
import * as Belt_SortArray from "rescript/lib/es6/belt_SortArray.js";

function head(x) {
if (x) {
Expand Down Expand Up @@ -1177,9 +1176,8 @@ function setAssoc(xs, x, k, eq) {
}

function sort(xs, cmp) {
var cmp$1 = Curry.__2(cmp);
var arr = toArray(xs);
Belt_SortArray.stableSortInPlaceByU(arr, cmp$1);
arr.sort(cmp);
return fromArray(arr);
}

Expand Down
6 changes: 2 additions & 4 deletions src/Core__List.res
Original file line number Diff line number Diff line change
Expand Up @@ -793,14 +793,12 @@ let setAssocU = (xs, x, k, eq) =>

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

let sortU = (xs, cmp) => {
let sort = (xs, cmp) => {
let arr = toArray(xs)
Belt_SortArray.stableSortInPlaceByU(arr, cmp)
Core__Array.sortInPlace(arr, cmp)
fromArray(arr)
}

let sort = (xs, cmp) => sortU(xs, (. x, y) => cmp(x, y))

let rec getByU = (xs, p) =>
switch xs {
| list{} => None
Expand Down