Skip to content

Commit 88ead4c

Browse files
authored
Use idiomatic new JS array immutable helpers (#139)
* Use idiomatic new JS array immutable helpers Fixes #138 Added `toSorted`, `toReversed`, `toSpliced` and `with`, per spec. Which means moving `sortInPlace` to `sort`. Same for `reverseInPlace` -> `reverse` and `spliceInPlace` -> `splice`. This is a small breaking change, thankfully caught by the type system, and by Core typing these mutable helpers' return as `unit`. Did the same for `TypedArray`. Though its `reverse` and `sort` _did_ return the array itself, so this breaking change should be carefuly advertized. The `toBla-ed` naming convention seems to be the way to go in JS in the future. We should consider that for us as well. Otherwise we'd end up diverging more and more when JS releases more immutable helpers.
1 parent 5fd9776 commit 88ead4c

File tree

6 files changed

+47
-58
lines changed

6 files changed

+47
-58
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ This standard library is based on `rescript-js`, but with the tweaks and modific
109109
- `findIndexOpt`/`lastIndexOf`/`indexOfOpt` are added, returning `None` instead of `-1` if the item searched for does not exist. These are in addition to `findIndex`/`lastIndexOf`, which still returns `-1` when the item you're looking for does not exist.
110110
- `getUnsafe` added (copied from `Belt`).
111111
- `setUnsafe` added (copied from `Belt`).
112-
- `reverse` added (copied from `Belt`), in addition to existing `reverseInPlace`. `reverseInPlace` is zero cost but does not produce a new array. `reverse` does produce a new array.
112+
- `sort`, `toSorted`, `reverse`, `toReversed`, `splice`, `toSpliced` are the same as their JS counterpart (mutable and immutable, respectively).
113113
- `keepMap` is added from `Belt`, but **renamed to `filterMap`**. Rationale: `filterMap` is closer to the JS convention of naming. It's also available in other languages like Rust. `keep` et al can confuse beginners, who're bound to be looking for `filter` style names since that's what JS has.
114114
- `shuffle` and `shuffleInPlace` are added (copied from `Belt`).
115115
- `flatMap` added (copied from `Belt`, but using native `map` and `concat` functions).

src/Core__Array.mjs

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

4141
}
4242

43-
function sort(arr, cmp) {
44-
var result = arr.slice();
45-
result.sort(cmp);
46-
return result;
47-
}
48-
4943
function reduce(arr, init, f) {
5044
return arr.reduce(f, init);
5145
}
@@ -76,15 +70,6 @@ function swapUnsafe(xs, i, j) {
7670
xs[j] = tmp;
7771
}
7872

79-
function reverse(xs) {
80-
var len = xs.length;
81-
var result = new Array(len);
82-
for(var i = 0; i < len; ++i){
83-
result[i] = xs[(len - 1 | 0) - i | 0];
84-
}
85-
return result;
86-
}
87-
8873
function shuffleInPlace(xs) {
8974
var len = xs.length;
9075
for(var i = 0; i < len; ++i){
@@ -141,15 +126,13 @@ function findMap(arr, f) {
141126
export {
142127
make ,
143128
fromInitializer ,
144-
sort ,
145129
indexOfOpt ,
146130
lastIndexOfOpt ,
147131
reduce ,
148132
reduceWithIndex ,
149133
reduceRight ,
150134
reduceRightWithIndex ,
151135
findIndexOpt ,
152-
reverse ,
153136
filterMap ,
154137
keepSome ,
155138
shuffle ,

src/Core__Array.res

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,18 @@ external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<
5151

5252
@variadic @send external pushMany: (array<'a>, array<'a>) => unit = "push"
5353

54-
@send external reverseInPlace: array<'a> => unit = "reverse"
54+
@send external reverse: array<'a> => unit = "reverse"
55+
@send external toReversed: array<'a> => array<'a> = "toReversed"
5556

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

5859
@variadic @send
59-
external spliceInPlace: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit =
60-
"splice"
60+
external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit = "splice"
61+
@variadic @send
62+
external toSpliced: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => array<'a> =
63+
"toSpliced"
64+
65+
@send external with: (array<'a>, int, 'a) => array<'a> = "with"
6166

6267
@send external unshift: (array<'a>, 'a) => unit = "unshift"
6368

@@ -92,13 +97,8 @@ let lastIndexOfOpt = (arr, item) =>
9297
@send external sliceToEnd: (array<'a>, ~start: int) => array<'a> = "slice"
9398
@send external copy: array<'a> => array<'a> = "slice"
9499

95-
@send external sortInPlace: (array<'a>, ('a, 'a) => int) => unit = "sort"
96-
97-
let sort = (arr, cmp) => {
98-
let result = copy(arr)
99-
sortInPlace(result, cmp)
100-
result
101-
}
100+
@send external sort: (array<'a>, ('a, 'a) => int) => unit = "sort"
101+
@send external toSorted: (array<'a>, ('a, 'a) => int) => array<'a> = "toSorted"
102102

103103
@send external toString: array<'a> => string = "toString"
104104
@send external toLocaleString: array<'a> => string = "toLocaleString"
@@ -154,15 +154,6 @@ let swapUnsafe = (xs, i, j) => {
154154
setUnsafe(xs, j, tmp)
155155
}
156156

157-
let reverse = xs => {
158-
let len = length(xs)
159-
let result = makeUninitializedUnsafe(len)
160-
for i in 0 to len - 1 {
161-
setUnsafe(result, i, getUnsafe(xs, len - 1 - i))
162-
}
163-
result
164-
}
165-
166157
let shuffleInPlace = xs => {
167158
let len = length(xs)
168159
for i in 0 to len - 1 {

src/Core__Array.resi

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Console.log(someArray) // ["hi", "hello", "yay", "wehoo"]
165165
external pushMany: (array<'a>, array<'a>) => unit = "push"
166166

167167
/**
168-
`reverseInPlace(array)` reverses the order of the items in the array.
168+
`reverse(array)` reverses the order of the items in `array`.
169169

170170
Beware this will *mutate* the array.
171171

@@ -174,13 +174,13 @@ See [`Array.reverse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re
174174
## Examples
175175
```rescript
176176
let someArray = ["hi", "hello"]
177-
someArray->Array.reverseInPlace
177+
someArray->Array.reverse
178178

179179
Console.log(someArray) // ["hello", "h1"]
180180
```
181181
*/
182182
@send
183-
external reverseInPlace: array<'a> => unit = "reverse"
183+
external reverse: array<'a> => unit = "reverse"
184184

185185
/**
186186
`shift(array)` removes the first item in the array, and returns it.
@@ -201,22 +201,24 @@ Console.log(someArray) // ["hello"]. Notice first item is gone.
201201
external shift: array<'a> => option<'a> = "shift"
202202

203203
/**
204-
`sort(array, comparator)` returns a new, sorted array from `array`, using the provided `comparator` function.
204+
`toSorted(array, comparator)` returns a new, sorted array from `array`, using the `comparator` function.
205205

206-
See [`Array.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) on MDN.
206+
See [`Array.toSorted`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted) on MDN.
207207

208208
## Examples
209209
```rescript
210210
let someArray = [3, 2, 1]
211-
let sortedArray = someArray->Array.sort((a, b) => a > b ? 1 : -1)
211+
let sorted = someArray->Array.toSorted((a, b) => a - b)
212212

213-
Console.log(sortedArray) // [1, 2, 3]
213+
Console.log(sorted) // [1, 2, 3]
214+
Console.log(someArray) // [3, 2, 1]. Original unchanged
214215
```
215216
*/
216-
let sort: (array<'a>, ('a, 'a) => int) => array<'a>
217+
@send
218+
external toSorted: (array<'a>, ('a, 'a) => int) => array<'a> = "toSorted"
217219

218220
/**
219-
`sortInPlace(array, comparator)` sorts `array` using the `comparator` function.
221+
`sort(array, comparator)` sorts `array` in-place using the `comparator` function.
220222

221223
Beware this will *mutate* the array.
222224

@@ -225,16 +227,22 @@ See [`Array.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
225227
## Examples
226228
```rescript
227229
let someArray = [3, 2, 1]
228-
someArray->Array.sortInPlace((a, b) => a > b ? 1 : -1)
230+
someArray->Array.sort((a, b) => a - b)
229231

230232
Console.log(someArray) // [1, 2, 3]
231233
```
232234
*/
233235
@send
234-
external sortInPlace: (array<'a>, ('a, 'a) => int) => unit = "sort"
236+
external sort: (array<'a>, ('a, 'a) => int) => unit = "sort"
237+
238+
@variadic @send
239+
external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit = "splice"
240+
235241
@variadic @send
236-
external spliceInPlace: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit =
237-
"splice"
242+
external toSpliced: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => array<'a> =
243+
"toSpliced"
244+
245+
@send external with: (array<'a>, int, 'a) => array<'a> = "with"
238246

239247
/**
240248
`unshift(array, item)` inserts a new item at the start of the array.
@@ -805,7 +813,7 @@ Console.log(array[1]) // "Hello"
805813
external setUnsafe: (array<'a>, int, 'a) => unit = "%array_unsafe_set"
806814

807815
/**
808-
`findIndexOpt(array, checker)` returns the index of the first element of `array` where the provided `checker` function returns true.
816+
`findIndexOpt(array, checker)` returns the index of the first element of `array` where the provided `checker` function returns true.
809817

810818
Returns `None` if no item matches.
811819

@@ -826,19 +834,21 @@ switch array->Array.findIndexOpt(item => item == ReScript) {
826834
let findIndexOpt: (array<'a>, 'a => bool) => option<int>
827835

828836
/**
829-
`reverse(array)` creates a new array with all items from `array` in reversed order.
837+
`toReversed(array)` creates a new array with all items from `array` in reversed order.
830838

831-
See [`Array.reverse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse) on MDN.
839+
See [`Array.toReversed`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed) on MDN.
832840

833841
## Examples
834842
```rescript
835843
let someArray = ["hi", "hello"]
836-
let reversed = someArray->Array.reverse
844+
let reversed = someArray->Array.toReversed
837845

838846
Console.log(reversed) // ["hello", "h1"]
847+
Console.log(someArray) // ["h1", "hello"]. Original unchanged
839848
```
840849
*/
841-
let reverse: array<'a> => array<'a>
850+
@send
851+
external toReversed: array<'a> => array<'a> = "toReversed"
842852

843853
/**
844854
`get(array, index)` returns the element at `index` of `array`.

src/Core__List.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ let setAssoc = (xs, x, k, eq) => setAssocU(xs, x, k, (. a, b) => eq(a, b))
795795

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

src/typed-arrays/Core__TypedArray.res

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ external copyWithin: (t<'a>, ~target: int, ~start: int, ~end: int) => array<'a>
2121
@send external fillInPlaceToEnd: (t<'a>, 'a, ~start: int) => t<'a> = "fill"
2222
@send external fillInPlace: (t<'a>, 'a, ~start: int, ~end: int) => t<'a> = "fill"
2323

24-
@send external reverseInPlace: t<'a> => t<'a> = "reverse"
25-
@send external sortInPlace: (t<'a>, ('a, 'a) => int) => t<'a> = "sort"
24+
@send external reverse: t<'a> => unit = "reverse"
25+
@send external toReversed: t<'a> => t<'a> = "toReversed"
26+
27+
@send external sort: (t<'a>, ('a, 'a) => int) => unit = "sort"
28+
@send external toSorted: (t<'a>, ('a, 'a) => int) => t<'a> = "toSorted"
29+
30+
@send external with: (t<'a>, int, 'a) => t<'a> = "with"
2631

2732
@send external includes: (t<'a>, 'a) => bool = "includes"
2833

0 commit comments

Comments
 (0)