Skip to content

Commit 5e9abe4

Browse files
cknittzth
andauthored
Align List api with other modules (#195)
* Align List api with other modules * Changelog --------- Co-authored-by: Gabriel Nordeborn <gabbe.nord@gmail.com>
1 parent efd4a8e commit 5e9abe4

File tree

4 files changed

+45
-44
lines changed

4 files changed

+45
-44
lines changed

CHANGELOG.md

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

33
## Next version
44

5+
- Align List api with other modules (`List.getBy` -> `List.find` etc.). https://github.com/rescript-association/rescript-core/pull/195
56
- BREAKING: Adds typed bindings to `Intl`, replacing the options type of `{..}` with records. https://github.com/rescript-association/rescript-core/pull/65
67
- Add `Dict.forEach`, `Dict.forEachWithKey` and `Dict.mapValues` https://github.com/rescript-association/rescript-core/pull/181
78
- Remove internal xxxU helper functions that are not needed anymore in uncurried mode. https://github.com/rescript-association/rescript-core/pull/191

src/Core__List.mjs

+11-11
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ function copyAuxWithMapI(f, _i, _cellX, _prec) {
370370
return ;
371371
}
372372
var next = {
373-
hd: f(i, cellX.hd),
373+
hd: f(cellX.hd, i),
374374
tl: /* [] */0
375375
};
376376
prec.tl = next;
@@ -541,14 +541,14 @@ function mapWithIndex(xs, f) {
541541
return /* [] */0;
542542
}
543543
var cell = {
544-
hd: f(0, xs.hd),
544+
hd: f(xs.hd, 0),
545545
tl: /* [] */0
546546
};
547547
copyAuxWithMapI(f, 1, xs.tl, cell);
548548
return cell;
549549
}
550550

551-
function makeBy(n, f) {
551+
function fromInitializer(n, f) {
552552
if (n <= 0) {
553553
return /* [] */0;
554554
}
@@ -672,7 +672,7 @@ function reverse(l) {
672672
return reverseConcat(l, /* [] */0);
673673
}
674674

675-
function flattenAux(_prec, _xs) {
675+
function flatAux(_prec, _xs) {
676676
while(true) {
677677
var xs = _xs;
678678
var prec = _prec;
@@ -686,7 +686,7 @@ function flattenAux(_prec, _xs) {
686686
};
687687
}
688688

689-
function flatten(_xs) {
689+
function flat(_xs) {
690690
while(true) {
691691
var xs = _xs;
692692
if (!xs) {
@@ -698,7 +698,7 @@ function flatten(_xs) {
698698
hd: match.hd,
699699
tl: /* [] */0
700700
};
701-
flattenAux(copyAuxCont(match.tl, cell), xs.tl);
701+
flatAux(copyAuxCont(match.tl, cell), xs.tl);
702702
return cell;
703703
}
704704
_xs = xs.tl;
@@ -761,7 +761,7 @@ function forEachWithIndex(l, f) {
761761
if (!xs) {
762762
return ;
763763
}
764-
f(i, xs.hd);
764+
f(xs.hd, i);
765765
_i = i + 1 | 0;
766766
_xs = xs.tl;
767767
continue ;
@@ -1134,7 +1134,7 @@ function sort(xs, cmp) {
11341134
return fromArray(arr);
11351135
}
11361136

1137-
function getBy(_xs, p) {
1137+
function find(_xs, p) {
11381138
while(true) {
11391139
var xs = _xs;
11401140
if (!xs) {
@@ -1301,15 +1301,15 @@ export {
13011301
get ,
13021302
getExn ,
13031303
make ,
1304-
makeBy ,
1304+
fromInitializer ,
13051305
toShuffled ,
13061306
drop ,
13071307
take ,
13081308
splitAt ,
13091309
concat ,
13101310
concatMany ,
13111311
reverseConcat ,
1312-
flatten ,
1312+
flat ,
13131313
map ,
13141314
zip ,
13151315
zipBy ,
@@ -1335,7 +1335,7 @@ export {
13351335
compare ,
13361336
equal ,
13371337
has ,
1338-
getBy ,
1338+
find ,
13391339
filter ,
13401340
filterWithIndex ,
13411341
filterMap ,

src/Core__List.res

+16-16
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ let rec copyAuxWithMap2 = (f, cellX, cellY, prec) =>
283283
let rec copyAuxWithMapI = (f, i, cellX, prec) =>
284284
switch cellX {
285285
| list{h, ...t} =>
286-
let next = mutableCell(f(i, h), list{})
286+
let next = mutableCell(f(h, i), list{})
287287
unsafeMutateTail(prec, next)
288288
copyAuxWithMapI(f, i + 1, t, next)
289289
| list{} => ()
@@ -401,12 +401,12 @@ let mapWithIndex = (xs, f) =>
401401
switch xs {
402402
| list{} => list{}
403403
| list{h, ...t} =>
404-
let cell = mutableCell(f(0, h), list{})
404+
let cell = mutableCell(f(h, 0), list{})
405405
copyAuxWithMapI(f, 1, t, cell)
406406
cell
407407
}
408408

409-
let makeBy = (n, f) =>
409+
let fromInitializer = (~length as n, f) =>
410410
if n <= 0 {
411411
list{}
412412
} else {
@@ -423,7 +423,7 @@ let makeBy = (n, f) =>
423423
headX
424424
}
425425

426-
let make = (type a, n, v: a): list<a> =>
426+
let make = (type a, ~length as n, v: a): list<a> =>
427427
if n <= 0 {
428428
list{}
429429
} else {
@@ -507,19 +507,19 @@ let rec reverseConcat = (l1, l2) =>
507507

508508
let reverse = l => reverseConcat(l, list{})
509509

510-
let rec flattenAux = (prec, xs) =>
510+
let rec flatAux = (prec, xs) =>
511511
switch xs {
512512
| list{} => unsafeMutateTail(prec, list{})
513-
| list{h, ...r} => flattenAux(copyAuxCont(h, prec), r)
513+
| list{h, ...r} => flatAux(copyAuxCont(h, prec), r)
514514
}
515515

516-
let rec flatten = xs =>
516+
let rec flat = xs =>
517517
switch xs {
518518
| list{} => list{}
519-
| list{list{}, ...xs} => flatten(xs)
519+
| list{list{}, ...xs} => flat(xs)
520520
| list{list{h, ...t}, ...r} =>
521521
let cell = mutableCell(h, list{})
522-
flattenAux(copyAuxCont(t, cell), r)
522+
flatAux(copyAuxCont(t, cell), r)
523523
cell
524524
}
525525

@@ -548,19 +548,19 @@ let rec forEach = (xs, f) =>
548548
switch xs {
549549
| list{} => ()
550550
| list{a, ...l} =>
551-
f(a)->ignore
551+
f(a)
552552
forEach(l, f)
553553
}
554554

555-
let rec iteri = (xs, i, f) =>
555+
let rec forEachWithIndexAux = (xs, f, i) =>
556556
switch xs {
557557
| list{} => ()
558558
| list{a, ...l} =>
559-
f(i, a)->ignore
560-
iteri(l, i + 1, f)
559+
f(a, i)
560+
forEachWithIndexAux(l, f, i + 1)
561561
}
562562

563-
let forEachWithIndex = (l, f) => iteri(l, 0, f)
563+
let forEachWithIndex = (l, f) => forEachWithIndexAux(l, f, 0)
564564

565565
let rec reduce = (l, accu, f) =>
566566
switch l {
@@ -751,14 +751,14 @@ let sort = (xs, cmp) => {
751751
fromArray(arr)
752752
}
753753

754-
let rec getBy = (xs, p) =>
754+
let rec find = (xs, p) =>
755755
switch xs {
756756
| list{} => None
757757
| list{x, ...l} =>
758758
if p(x) {
759759
Some(x)
760760
} else {
761-
getBy(l, p)
761+
find(l, p)
762762
}
763763
}
764764

src/Core__List.resi

+17-17
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ with `value`. Returns an empty list if `value` is negative.
173173
## Examples
174174

175175
```rescript
176-
List.make(3, 1) // list{1, 1, 1}
176+
List.make(~length=3, 1) // list{1, 1, 1}
177177
```
178178
*/
179-
let make: (int, 'a) => t<'a>
179+
let make: (~length: int, 'a) => t<'a>
180180

181181
/**
182182
`makeBy(length, f)` return a list of length `length` with element initialized
@@ -185,12 +185,12 @@ with `f`. Returns an empty list if `length` is negative.
185185
## Examples
186186

187187
```rescript
188-
List.makeBy(5, i => i) // list{0, 1, 2, 3, 4}
188+
List.fromInitializer(5, i => i) // list{0, 1, 2, 3, 4}
189189

190-
List.makeBy(5, i => i * i) // list{0, 1, 4, 9, 16}
190+
List.fromInitializer(5, i => i * i) // list{0, 1, 4, 9, 16}
191191
```
192192
*/
193-
let makeBy: (int, int => 'a) => t<'a>
193+
let fromInitializer: (~length: int, int => 'a) => t<'a>
194194

195195
/**
196196
`toShuffled(list)` returns a new list in random order.
@@ -284,16 +284,16 @@ List.reverseConcat(list{1, 2}, list{3, 4}) // list{2, 1, 3, 4}
284284
let reverseConcat: (t<'a>, t<'a>) => t<'a>
285285

286286
/**
287-
`flatten(list)` return the list obtained by concatenating all the lists in
287+
`flat(list)` return the list obtained by concatenating all the lists in
288288
`list`, in order.
289289

290290
## Examples
291291

292292
```rescript
293-
List.flatten(list{list{1, 2, 3}, list{}, list{3}}) // list{1, 2, 3, 3}
293+
List.flat(list{list{1, 2, 3}, list{}, list{3}}) // list{1, 2, 3, 3}
294294
```
295295
*/
296-
let flatten: t<t<'a>> => t<'a>
296+
let flat: t<t<'a>> => t<'a>
297297

298298
/**
299299
`map(list, f)` returns a new list with `f` applied to each element of `list`.
@@ -337,10 +337,10 @@ that order.
337337
## Examples
338338

339339
```rescript
340-
list{1, 2, 3}->List.mapWithIndex((index, x) => index + x) // list{1, 3, 5}
340+
list{1, 2, 3}->List.mapWithIndex((x, index) => index + x) // list{1, 3, 5}
341341
```
342342
*/
343-
let mapWithIndex: (t<'a>, (int, 'a) => 'b) => t<'b>
343+
let mapWithIndex: (t<'a>, ('a, int) => 'b) => t<'b>
344344

345345
/**
346346
`fromArray(arr)` converts the given array `arr` to a list.
@@ -415,7 +415,7 @@ List.forEach(list{"a", "b", "c"}, x => Console.log("Item: " ++ x))
415415
*/
416416
```
417417
*/
418-
let forEach: (t<'a>, 'a => 'b) => unit
418+
let forEach: (t<'a>, 'a => unit) => unit
419419

420420
/**
421421
`forEachWithIndex(list, f, index)` call `f` on each element of `list` from beginning
@@ -425,7 +425,7 @@ element from `list`. `f` returns `unit`.
425425
## Examples
426426

427427
```rescript
428-
List.forEachWithIndex(list{"a", "b", "c"}, (index, x) => {
428+
List.forEachWithIndex(list{"a", "b", "c"}, (x, index) => {
429429
Console.log("Item " ++ Int.toString(index) ++ " is " ++ x)
430430
})
431431
/*
@@ -436,7 +436,7 @@ List.forEachWithIndex(list{"a", "b", "c"}, (index, x) => {
436436
*/
437437
```
438438
*/
439-
let forEachWithIndex: (t<'a>, (int, 'a) => 'b) => unit
439+
let forEachWithIndex: (t<'a>, ('a, int) => unit) => unit
440440

441441
/**
442442
`reduce(list, initialValue, f)` applies `f` to each element of `list` from
@@ -693,19 +693,19 @@ list{(-1), (-2), (-3)}->List.has(2, (a, b) => abs(a) == abs(b)) // true
693693
let has: (t<'a>, 'b, ('a, 'b) => bool) => bool
694694

695695
/**
696-
`getBy(list, f)` returns `Some(value)` for the first value in `list` that
696+
`find(list, f)` returns `Some(value)` for the first value in `list` that
697697
satisfies the predicate function `f`. Returns `None` if no element satisfies
698698
the function.
699699

700700
## Examples
701701

702702
```rescript
703-
List.getBy(list{1, 4, 3, 2}, x => x > 3) // Some(4)
703+
List.find(list{1, 4, 3, 2}, x => x > 3) // Some(4)
704704

705-
List.getBy(list{1, 4, 3, 2}, x => x > 4) // None
705+
List.find(list{1, 4, 3, 2}, x => x > 4) // None
706706
```
707707
*/
708-
let getBy: (t<'a>, 'a => bool) => option<'a>
708+
let find: (t<'a>, 'a => bool) => option<'a>
709709

710710
/**
711711
`filter(list, f)` returns a list of all elements in `list` which satisfy the

0 commit comments

Comments
 (0)