Skip to content

Commit 5ac2680

Browse files
committed
Change idiom from e.g. shuffle to toShuffled and shuffleInPlace to shuffle
See #139 (comment)
1 parent 88ead4c commit 5ac2680

12 files changed

+75
-49
lines changed

CHANGELOG.md

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

33
## main
44

5+
## Next version
6+
7+
### API changes
8+
9+
- `Array` mutable & immutable helper name changed to conform to JS' upcoming APIs [such as `toSorted`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted)
10+
- `sort` -> `toSorted`, `sortInPlace` -> `sort`
11+
- `reverse` -> `toReversed`, `reverseInPlace` -> `reverse`
12+
- `splice` -> `toSpliced`, `spliceInPlace` -> `splice`
13+
- `shuffle` -> `toShuffled`, `shuffleInPlace` -> `shuffle`
14+
- `fillAllInPlace` -> `fillAll`, `fillInPlaceToEnd` -> `fillToEnd`, `fillInPlace` -> `fill`
15+
- added `with`
16+
- Same for `TypedArray`:
17+
- `sort` -> `toSorted`, `sortInPlace` -> `sort`
18+
- `reverse` -> `toReversed`, `reverseInPlace` -> `reverse`
19+
- `fillAllInPlace` -> `fillAll`, `fillInPlaceToEnd` -> `fillToEnd`, `fillInPlace` -> `fill`
20+
- And `List:
21+
- `shuffle` -> `toShuffled`
22+
23+
**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.
24+
25+
**Note 2**: the newly added helpers, `Array.toSorted`, `Array.toSpliced`, `Array.toReversed`, `Array.with`, `TypedArray.toSorted` and `TypedArray.toReversed` require their respective polyfill, as [they're not currently supported by Firefox](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted).
26+
527
## 0.3.1
628

729
### Bug fixes

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ This standard library is based on `rescript-js`, but with the tweaks and modific
111111
- `setUnsafe` added (copied from `Belt`).
112112
- `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.
114-
- `shuffle` and `shuffleInPlace` are added (copied from `Belt`).
114+
- `shuffle` and `toShuffled` are added (copied from `Belt`'s `shuffleInPlace` and `shuffle`).
115115
- `flatMap` added (copied from `Belt`, but using native `map` and `concat` functions).
116116

117117
### Float

migration/migration.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ match="Math.min_float"
150150
rewrite="Math.min"
151151

152152
## BELT
153-
## Below are migrations that cover moving from Belt to equivalents
153+
## Below are migrations that cover moving from Belt to equivalents
154154
## available in the new stdlib.
155155
[belt-option]
156156
match="Belt.Option"
@@ -182,6 +182,10 @@ rewrite="Array.filterMap"
182182

183183
[belt-array-a-shuffle]
184184
match="Belt.Array.shuffle"
185+
rewrite="Array.toShuffled"
186+
187+
[belt-array-a-shuffle-in-place]
188+
match="Belt.Array.shuffleInPlace"
185189
rewrite="Array.shuffle"
186190

187191
[belt-array-a-flat-map]
@@ -270,4 +274,4 @@ rewrite="Date"
270274

271275
[js-global-a]
272276
match="Js.Global."
273-
rewrite=""
277+
rewrite=""

src/Core__Array.mjs

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ function swapUnsafe(xs, i, j) {
7070
xs[j] = tmp;
7171
}
7272

73-
function shuffleInPlace(xs) {
73+
function shuffle(xs) {
7474
var len = xs.length;
7575
for(var i = 0; i < len; ++i){
7676
swapUnsafe(xs, i, Js_math.random_int(i, len));
7777
}
7878
}
7979

80-
function shuffle(xs) {
80+
function toShuffled(xs) {
8181
var result = xs.slice();
82-
shuffleInPlace(result);
82+
shuffle(result);
8383
return result;
8484
}
8585

@@ -135,8 +135,8 @@ export {
135135
findIndexOpt ,
136136
filterMap ,
137137
keepSome ,
138+
toShuffled ,
138139
shuffle ,
139-
shuffleInPlace ,
140140
findMap ,
141141
}
142142
/* No side effect */

src/Core__Array.res

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ external setUnsafe: (array<'a>, int, 'a) => unit = "%array_unsafe_set"
77
@val
88
external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> = "Array.from"
99

10-
@send external fillAllInPlace: (array<'a>, 'a) => unit = "fill"
10+
@send external fillAll: (array<'a>, 'a) => unit = "fill"
1111

12-
@send external fillInPlaceToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"
12+
@send external fillToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"
1313

14-
@send external fillInPlace: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill"
14+
@send external fill: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill"
1515

1616
let make = (~length, x) =>
1717
if length <= 0 {
1818
[]
1919
} else {
2020
let arr = makeUninitializedUnsafe(length)
21-
arr->fillAllInPlace(x)
21+
arr->fillAll(x)
2222
arr
2323
}
2424

@@ -154,16 +154,16 @@ let swapUnsafe = (xs, i, j) => {
154154
setUnsafe(xs, j, tmp)
155155
}
156156

157-
let shuffleInPlace = xs => {
157+
let shuffle = xs => {
158158
let len = length(xs)
159159
for i in 0 to len - 1 {
160160
swapUnsafe(xs, i, Js.Math.random_int(i, len)) /* [i,len) */
161161
}
162162
}
163163

164-
let shuffle = xs => {
164+
let toShuffled = xs => {
165165
let result = copy(xs)
166-
shuffleInPlace(result)
166+
shuffle(result)
167167
result
168168
}
169169

src/Core__Array.resi

+14-14
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ external copyWithinToEnd: (array<'a>, ~target: int, ~start: int) => array<'a> =
5656
external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<'a> = "copyWithin"
5757

5858
/**
59-
`fillAllInPlace(array, value)` fills the entire `array` with `value`.
59+
`fillAll(array, value)` fills the entire `array` with `value`.
6060

6161
Beware this will *mutate* the array.
6262

@@ -65,16 +65,16 @@ See [`Array.fill`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
6565
## Examples
6666
```rescript
6767
let myArray = [1, 2, 3, 4]
68-
myArray->Array.fillAllInPlace(9)
68+
myArray->Array.fillAll(9)
6969

7070
Console.log(myArray) // [9, 9, 9, 9]
7171
```
7272
*/
7373
@send
74-
external fillAllInPlace: (array<'a>, 'a) => unit = "fill"
74+
external fillAll: (array<'a>, 'a) => unit = "fill"
7575

7676
/**
77-
`fillInPlaceToEnd(array, value, ~start)` fills `array` with `value` from the `start` index.
77+
`fillToEnd(array, value, ~start)` fills `array` with `value` from the `start` index.
7878

7979
Beware this will *mutate* the array.
8080

@@ -83,16 +83,16 @@ See [`Array.fill`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
8383
## Examples
8484
```rescript
8585
let myArray = [1, 2, 3, 4]
86-
myArray->Array.fillInPlaceToEnd(9, ~start=1)
86+
myArray->Array.fillToEnd(9, ~start=1)
8787

8888
Console.log(myArray) // [1, 9, 9, 9]
8989
```
9090
*/
9191
@send
92-
external fillInPlaceToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"
92+
external fillToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"
9393

9494
/**
95-
`fillInPlace(array, value, ~start, ~end)` fills `array` with `value` from `start` to `end`.
95+
`fill(array, value, ~start, ~end)` fills `array` with `value` from `start` to `end`.
9696

9797
Beware this will *mutate* the array.
9898

@@ -101,13 +101,13 @@ See [`Array.fill`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
101101
## Examples
102102
```rescript
103103
let myArray = [1, 2, 3, 4]
104-
myArray->Array.fillInPlace(9, ~start=1, ~end=2)
104+
myArray->Array.fill(9, ~start=1, ~end=2)
105105

106106
Console.log(myArray) // [1, 9, 9, 4]
107107
```
108108
*/
109109
@send
110-
external fillInPlace: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill"
110+
external fill: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill"
111111

112112
/**
113113
`pop(array)` removes the last item from `array` and returns it.
@@ -885,20 +885,20 @@ let filterMap: (array<'a>, 'a => option<'b>) => array<'b>
885885
let keepSome: array<option<'a>> => array<'a>
886886

887887
/**
888-
`shuffle(array)` returns a new array with all items in `array` in a random order.
888+
`toShuffled(array)` returns a new array with all items in `array` in a random order.
889889

890890
## Examples
891891
```rescript
892892
let array = ["Hello", "Hi", "Good bye"]
893-
let shuffledArray = array->Array.shuffle
893+
let shuffledArray = array->Array.toShuffled
894894

895895
Console.log(shuffledArray)
896896
```
897897
*/
898-
let shuffle: array<'a> => array<'a>
898+
let toShuffled: array<'a> => array<'a>
899899

900900
/**
901-
`shuffleInPlace(array)` randomizes the position of all items in `array`.
901+
`shuffle(array)` randomizes the position of all items in `array`.
902902

903903
Beware this will *mutate* the array.
904904

@@ -910,7 +910,7 @@ array->Array.shuffle
910910
Console.log(array)
911911
```
912912
*/
913-
let shuffleInPlace: array<'a> => unit
913+
let shuffle: array<'a> => unit
914914

915915
/**
916916
`flatMap(array, mapper)` returns a new array concatenating the arrays returned from running `mapper` on all items in `array`.

src/Core__List.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,9 @@ function toArray(x) {
651651
return arr;
652652
}
653653

654-
function shuffle(xs) {
654+
function toShuffled(xs) {
655655
var v = toArray(xs);
656-
Core__Array.shuffleInPlace(v);
656+
Core__Array.shuffle(v);
657657
return fromArray(v);
658658
}
659659

@@ -1357,7 +1357,7 @@ export {
13571357
getExn ,
13581358
make ,
13591359
makeBy ,
1360-
shuffle ,
1360+
toShuffled ,
13611361
drop ,
13621362
take ,
13631363
splitAt ,

src/Core__List.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,9 @@ let toArray = (x: t<_>) => {
481481
arr
482482
}
483483

484-
let shuffle = xs => {
484+
let toShuffled = xs => {
485485
let v = toArray(xs)
486-
Core__Array.shuffleInPlace(v)
486+
Core__Array.shuffle(v)
487487
fromArray(v)
488488
}
489489

src/Core__List.resi

+4-4
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,15 @@ List.makeBy(5, i => i * i) // list{0, 1, 4, 9, 16}
193193
let makeBy: (int, int => 'a) => t<'a>
194194

195195
/**
196-
`shuffle(list)` returns a new list in random order.
196+
`toShuffled(list)` returns a new list in random order.
197197

198198
## Examples
199199

200200
```rescript
201-
List.shuffle(list{1, 2, 3}) // list{2, 1, 3}
201+
List.toShuffled(list{1, 2, 3}) // list{2, 1, 3}
202202
```
203203
*/
204-
let shuffle: t<'a> => t<'a>
204+
let toShuffled: t<'a> => t<'a>
205205

206206
/**
207207
`drop(list, value)` return a new list, dropping the first `value` element.
@@ -391,7 +391,7 @@ let f = x => x * x
391391
let l = list{3, 4, 5}
392392

393393
let withMap = List.map(l, f)->List.reverse
394-
let withMapReverse = l->List.mapReverse(f)
394+
let withMapReverse = l->List.mapReverse(f)
395395

396396
Console.log(withMap == withMapReverse) // true
397397
```

src/typed-arrays/Core__TypedArray.res

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ type t<'a>
1717
@send
1818
external copyWithin: (t<'a>, ~target: int, ~start: int, ~end: int) => array<'a> = "copyWithin"
1919

20-
@send external fillAllInPlace: (t<'a>, 'a) => t<'a> = "fill"
21-
@send external fillInPlaceToEnd: (t<'a>, 'a, ~start: int) => t<'a> = "fill"
22-
@send external fillInPlace: (t<'a>, 'a, ~start: int, ~end: int) => t<'a> = "fill"
20+
@send external fillAll: (t<'a>, 'a) => t<'a> = "fill"
21+
@send external fillToEnd: (t<'a>, 'a, ~start: int) => t<'a> = "fill"
22+
@send external fill: (t<'a>, 'a, ~start: int, ~end: int) => t<'a> = "fill"
2323

2424
@send external reverse: t<'a> => unit = "reverse"
2525
@send external toReversed: t<'a> => t<'a> = "toReversed"

test/ArrayTests.mjs

+6-6
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ Test.run([
201201
"ArrayTests.res",
202202
51,
203203
20,
204-
38
204+
41
205205
],
206-
"shuffle - length"
207-
], Core__Array.shuffle([
206+
"toShuffled - length"
207+
], Core__Array.toShuffled([
208208
1,
209209
2,
210210
3
@@ -221,10 +221,10 @@ Test.run([
221221
"ArrayTests.res",
222222
54,
223223
13,
224-
38
224+
31
225225
],
226-
"shuffleInPlace - length"
227-
], (Core__Array.shuffleInPlace(arr), arr.length), eq, 3);
226+
"shuffle - length"
227+
], (Core__Array.shuffle(arr), arr.length), eq, 3);
228228

229229
Test.run([
230230
[

test/ArrayTests.res

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ Test.run(
4848
list{},
4949
)
5050

51-
Test.run(__POS_OF__("shuffle - length"), Array.shuffle([1, 2, 3])->Array.length, eq, 3)
51+
Test.run(__POS_OF__("toShuffled - length"), Array.toShuffled([1, 2, 3])->Array.length, eq, 3)
5252

5353
Test.run(
54-
__POS_OF__("shuffleInPlace - length"),
54+
__POS_OF__("shuffle - length"),
5555
{
5656
let arr = [1, 2, 3]
57-
Array.shuffleInPlace(arr)
57+
Array.shuffle(arr)
5858
arr->Array.length
5959
},
6060
eq,

0 commit comments

Comments
 (0)