Skip to content
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

Use float for ordering, add Ordering module #149

Merged
merged 6 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
- `fillAllInPlace` -> `fillAll`, `fillInPlaceToEnd` -> `fillToEnd`, `fillInPlace` -> `fill`
- And `List`:
- `shuffle` -> `toShuffled`
- Use `float` instead of `int` for ordering to avoid premature overflow. https://github.com/rescript-association/rescript-core/pull/149
- Add `Ordering` module. https://github.com/rescript-association/rescript-core/pull/149

**Note 1**: These changes should all produce the correct type errors. Though `TypedArray`'s `reverse` and `sort` previously mutated _and_ returned the mutated array itself, whereas now they'd be copies. Please be careful refactoring these 2.

Expand Down
9 changes: 5 additions & 4 deletions src/Core__Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as Curry from "rescript/lib/es6/curry.js";
import * as Js_math from "rescript/lib/es6/js_math.js";
import * as Caml_option from "rescript/lib/es6/caml_option.js";
import * as Core__Ordering from "./Core__Ordering.mjs";

function make(length, x) {
if (length <= 0) {
Expand Down Expand Up @@ -47,10 +48,10 @@ function equal(a, b, eq) {
function compare(a, b, cmp) {
var lenA = a.length;
var lenB = b.length;
if (lenA > lenB) {
return 1;
} else if (lenA < lenB) {
if (lenA < lenB) {
return -1;
} else if (lenA > lenB) {
return 1;
} else {
var _i = 0;
while(true) {
Expand All @@ -59,7 +60,7 @@ function compare(a, b, cmp) {
return 0;
}
var c = Curry._2(cmp, a[i], b[i]);
if (c !== 0) {
if (!Core__Ordering.isEqual(c)) {
return c;
}
_i = i + 1 | 0;
Expand Down
20 changes: 9 additions & 11 deletions src/Core__Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ let equal = (a, b, eq) => {

let rec compareFromIndex = (a, b, i, cmp, len) =>
if i === len {
0
Core__Ordering.equal
} else {
let c = cmp(a->getUnsafe(i), b->getUnsafe(i))
if c === 0 {
if c->Core__Ordering.isEqual {
glennsl marked this conversation as resolved.
Show resolved Hide resolved
compareFromIndex(a, b, i + 1, cmp, len)
} else {
c
Expand All @@ -70,13 +70,11 @@ let rec compareFromIndex = (a, b, i, cmp, len) =>
let compare = (a, b, cmp) => {
let lenA = a->length
let lenB = b->length
if lenA > lenB {
1
} else if lenA < lenB {
-1
} else {
compareFromIndex(a, b, 0, cmp, lenA)
}
lenA < lenB
? Core__Ordering.less
: lenA > lenB
? Core__Ordering.greater
: compareFromIndex(a, b, 0, cmp, lenA)
}

@send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin"
Expand Down Expand Up @@ -139,8 +137,8 @@ let lastIndexOfOpt = (arr, item) =>
@send external sliceToEnd: (array<'a>, ~start: int) => array<'a> = "slice"
@send external copy: array<'a> => array<'a> = "slice"

@send external sort: (array<'a>, ('a, 'a) => int) => unit = "sort"
@send external toSorted: (array<'a>, ('a, 'a) => int) => array<'a> = "toSorted"
@send external sort: (array<'a>, ('a, 'a) => Core__Ordering.t) => unit = "sort"
@send external toSorted: (array<'a>, ('a, 'a) => Core__Ordering.t) => array<'a> = "toSorted"

@send external toString: array<'a> => string = "toString"
@send external toLocaleString: array<'a> => string = "toLocaleString"
Expand Down
10 changes: 5 additions & 5 deletions src/Core__Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let fromInitializer: (~length: int, int => 'a) => array<'a>

let equal: (array<'a>, array<'a>, ('a, 'a) => bool) => bool

let compare: (array<'a>, array<'a>, ('a, 'a) => int) => int
let compare: (array<'a>, array<'a>, ('a, 'a) => Core__Ordering.t) => Core__Ordering.t

@val external isArray: 'a => bool = "Array.isArray"

Expand Down Expand Up @@ -212,14 +212,14 @@ See [`Array.toSorted`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/R
## Examples
```rescript
let someArray = [3, 2, 1]
let sorted = someArray->Array.toSorted((a, b) => a - b)
let sorted = someArray->Array.toSorted(Int.compare)

Console.log(sorted) // [1, 2, 3]
Console.log(someArray) // [3, 2, 1]. Original unchanged
```
*/
@send
external toSorted: (array<'a>, ('a, 'a) => int) => array<'a> = "toSorted"
external toSorted: (array<'a>, ('a, 'a) => Core__Ordering.t) => array<'a> = "toSorted"

/**
`sort(array, comparator)` sorts `array` in-place using the `comparator` function.
Expand All @@ -231,13 +231,13 @@ See [`Array.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
## Examples
```rescript
let someArray = [3, 2, 1]
someArray->Array.sort((a, b) => a - b)
someArray->Array.sort((a, b) => float(a - b))

Console.log(someArray) // [1, 2, 3]
```
*/
@send
external sort: (array<'a>, ('a, 'a) => int) => unit = "sort"
external sort: (array<'a>, ('a, 'a) => Core__Ordering.t) => unit = "sort"

@variadic @send
external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit = "splice"
Expand Down
Loading