Skip to content

Commit 728e8da

Browse files
cknittzth
authored andcommitted
Align List api with other modules (rescript-lang#195)
* Align List api with other modules * Changelog --------- Co-authored-by: Gabriel Nordeborn <[email protected]>
1 parent 09ca99b commit 728e8da

File tree

4 files changed

+152
-66
lines changed

4 files changed

+152
-66
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- BREAKING: Fixes the type of `RegExp.Result.t` to be `array<option<string>>` instead of `array<string>`. https://github.com/rescript-association/rescript-core/pull/233.
66
- BREAKING: Adds typed bindings to `Intl`, replacing the options type of `{..}` with records. https://github.com/rescript-association/rescript-core/pull/65
7+
- Align List api with other modules (`List.getBy` -> `List.find` etc.). https://github.com/rescript-association/rescript-core/pull/195
78
- Add `Dict.forEach`, `Dict.forEachWithKey` and `Dict.mapValues` https://github.com/rescript-association/rescript-core/pull/181
89
- Remove internal xxxU helper functions that are not needed anymore in uncurried mode. https://github.com/rescript-association/rescript-core/pull/191
910
- Rename `Object.empty` to `Object.make` for consistency.

src/Core__List.mjs

+48-32
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ function copyAuxWithMapI(f, _i, _cellX, _prec) {
371371
return ;
372372
}
373373
var next = {
374-
hd: f(i, cellX.hd),
374+
hd: f(cellX.hd, i),
375375
tl: /* [] */0
376376
};
377377
prec.tl = next;
@@ -545,27 +545,26 @@ function mapWithIndex(xs, f) {
545545
return /* [] */0;
546546
}
547547
var cell = {
548-
hd: f$1(0, xs.hd),
548+
hd: f$1(xs.hd, 0),
549549
tl: /* [] */0
550550
};
551551
copyAuxWithMapI(f$1, 1, xs.tl, cell);
552552
return cell;
553553
}
554554

555-
function makeBy(n, f) {
556-
var f$1 = Curry.__1(f);
555+
function fromInitializerU(n, f) {
557556
if (n <= 0) {
558557
return /* [] */0;
559558
}
560559
var headX = {
561-
hd: f$1(0),
560+
hd: f(0),
562561
tl: /* [] */0
563562
};
564563
var cur = headX;
565564
var i = 1;
566565
while(i < n) {
567566
var v = {
568-
hd: f$1(i),
567+
hd: f(i),
569568
tl: /* [] */0
570569
};
571570
cur.tl = v;
@@ -575,6 +574,10 @@ function makeBy(n, f) {
575574
return headX;
576575
}
577576

577+
function fromInitializer(n, f) {
578+
return fromInitializerU(n, Curry.__1(f));
579+
}
580+
578581
function make(n, v) {
579582
if (n <= 0) {
580583
return /* [] */0;
@@ -677,7 +680,7 @@ function reverse(l) {
677680
return reverseConcat(l, /* [] */0);
678681
}
679682

680-
function flattenAux(_prec, _xs) {
683+
function flatAux(_prec, _xs) {
681684
while(true) {
682685
var xs = _xs;
683686
var prec = _prec;
@@ -691,7 +694,7 @@ function flattenAux(_prec, _xs) {
691694
};
692695
}
693696

694-
function flatten(_xs) {
697+
function flat(_xs) {
695698
while(true) {
696699
var xs = _xs;
697700
if (!xs) {
@@ -703,7 +706,7 @@ function flatten(_xs) {
703706
hd: match.hd,
704707
tl: /* [] */0
705708
};
706-
flattenAux(copyAuxCont(match.tl, cell), xs.tl);
709+
flatAux(copyAuxCont(match.tl, cell), xs.tl);
707710
return cell;
708711
}
709712
_xs = xs.tl;
@@ -746,37 +749,44 @@ function mapReverse(l, f) {
746749
};
747750
}
748751

749-
function forEach(xs, f) {
750-
var _xs = xs;
751-
var f$1 = Curry.__1(f);
752+
function forEachU(_xs, f) {
752753
while(true) {
753-
var xs$1 = _xs;
754-
if (!xs$1) {
754+
var xs = _xs;
755+
if (!xs) {
755756
return ;
756757
}
757-
f$1(xs$1.hd);
758-
_xs = xs$1.tl;
758+
f(xs.hd);
759+
_xs = xs.tl;
759760
continue ;
760761
};
761762
}
762763

763-
function forEachWithIndex(l, f) {
764-
var _xs = l;
765-
var _i = 0;
766-
var f$1 = Curry.__2(f);
764+
function forEach(xs, f) {
765+
forEachU(xs, Curry.__1(f));
766+
}
767+
768+
function forEachWithIndexAux(_xs, _i, f) {
767769
while(true) {
768770
var i = _i;
769771
var xs = _xs;
770772
if (!xs) {
771773
return ;
772774
}
773-
f$1(i, xs.hd);
775+
f(xs.hd, i);
774776
_i = i + 1 | 0;
775777
_xs = xs.tl;
776778
continue ;
777779
};
778780
}
779781

782+
function forEachWithIndexU(l, f) {
783+
forEachWithIndexAux(l, 0, f);
784+
}
785+
786+
function forEachWithIndex(l, f) {
787+
forEachWithIndexAux(l, 0, Curry.__2(f));
788+
}
789+
780790
function reduce(l, accu, f) {
781791
var _l = l;
782792
var _accu = accu;
@@ -1175,23 +1185,25 @@ function sort(xs, cmp) {
11751185
return fromArray(arr);
11761186
}
11771187

1178-
function getBy(xs, p) {
1179-
var _xs = xs;
1180-
var p$1 = Curry.__1(p);
1188+
function findU(_xs, p) {
11811189
while(true) {
1182-
var xs$1 = _xs;
1183-
if (!xs$1) {
1190+
var xs = _xs;
1191+
if (!xs) {
11841192
return ;
11851193
}
1186-
var x = xs$1.hd;
1187-
if (p$1(x)) {
1194+
var x = xs.hd;
1195+
if (p(x)) {
11881196
return Caml_option.some(x);
11891197
}
1190-
_xs = xs$1.tl;
1198+
_xs = xs.tl;
11911199
continue ;
11921200
};
11931201
}
11941202

1203+
function find(xs, p) {
1204+
return findU(xs, Curry.__1(p));
1205+
}
1206+
11951207
function filter(xs, p) {
11961208
var _xs = xs;
11971209
var p$1 = Curry.__1(p);
@@ -1350,15 +1362,16 @@ export {
13501362
get ,
13511363
getExn ,
13521364
make ,
1353-
makeBy ,
1365+
fromInitializerU ,
1366+
fromInitializer ,
13541367
toShuffled ,
13551368
drop ,
13561369
take ,
13571370
splitAt ,
13581371
concat ,
13591372
concatMany ,
13601373
reverseConcat ,
1361-
flatten ,
1374+
flat ,
13621375
map ,
13631376
zip ,
13641377
zipBy ,
@@ -1367,7 +1380,9 @@ export {
13671380
toArray ,
13681381
reverse ,
13691382
mapReverse ,
1383+
forEachU ,
13701384
forEach ,
1385+
forEachWithIndexU ,
13711386
forEachWithIndex ,
13721387
reduce ,
13731388
reduceWithIndex ,
@@ -1384,7 +1399,8 @@ export {
13841399
compare ,
13851400
equal ,
13861401
has ,
1387-
getBy ,
1402+
findU ,
1403+
find ,
13881404
filter ,
13891405
filterWithIndex ,
13901406
filterMap ,

src/Core__List.res

+17-17
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{} => ()
@@ -405,14 +405,14 @@ let mapWithIndexU = (xs, f) =>
405405
switch xs {
406406
| list{} => list{}
407407
| list{h, ...t} =>
408-
let cell = mutableCell(f(. 0, h), list{})
408+
let cell = mutableCell(f(. h, 0), list{})
409409
copyAuxWithMapI(f, 1, t, cell)
410410
cell
411411
}
412412

413413
let mapWithIndex = (xs, f) => mapWithIndexU(xs, (. i, x) => f(i, x))
414414

415-
let makeByU = (n, f) =>
415+
let fromInitializerU = (n, f) =>
416416
if n <= 0 {
417417
list{}
418418
} else {
@@ -429,7 +429,7 @@ let makeByU = (n, f) =>
429429
headX
430430
}
431431

432-
let makeBy = (n, f) => makeByU(n, (. x) => f(x))
432+
let fromInitializer = (n, f) => fromInitializerU(n, (. x) => f(x))
433433

434434
let make = (type a, n, v: a): list<a> =>
435435
if n <= 0 {
@@ -515,19 +515,19 @@ let rec reverseConcat = (l1, l2) =>
515515

516516
let reverse = l => reverseConcat(l, list{})
517517

518-
let rec flattenAux = (prec, xs) =>
518+
let rec flatAux = (prec, xs) =>
519519
switch xs {
520520
| list{} => unsafeMutateTail(prec, list{})
521-
| list{h, ...r} => flattenAux(copyAuxCont(h, prec), r)
521+
| list{h, ...r} => flatAux(copyAuxCont(h, prec), r)
522522
}
523523

524-
let rec flatten = xs =>
524+
let rec flat = xs =>
525525
switch xs {
526526
| list{} => list{}
527-
| list{list{}, ...xs} => flatten(xs)
527+
| list{list{}, ...xs} => flat(xs)
528528
| list{list{h, ...t}, ...r} =>
529529
let cell = mutableCell(h, list{})
530-
flattenAux(copyAuxCont(t, cell), r)
530+
flatAux(copyAuxCont(t, cell), r)
531531
cell
532532
}
533533

@@ -564,16 +564,16 @@ let rec forEachU = (xs, f) =>
564564

565565
let forEach = (xs, f) => forEachU(xs, (. x) => f(x))
566566

567-
let rec iteri = (xs, i, f) =>
567+
let rec forEachWithIndexAux = (xs, i, f) =>
568568
switch xs {
569569
| list{} => ()
570570
| list{a, ...l} =>
571-
f(. i, a)->ignore
572-
iteri(l, i + 1, f)
571+
f(. a, i)
572+
forEachWithIndexAux(l, i + 1, f)
573573
}
574574

575-
let forEachWithIndexU = (l, f) => iteri(l, 0, f)
576-
let forEachWithIndex = (l, f) => forEachWithIndexU(l, (. i, x) => f(i, x))
575+
let forEachWithIndexU = (l, f) => forEachWithIndexAux(l, 0, f)
576+
let forEachWithIndex = (l, f) => forEachWithIndexAux(l, 0, (. x, i) => f(x, i))
577577

578578
let rec reduceU = (l, accu, f) =>
579579
switch l {
@@ -796,18 +796,18 @@ let sort = (xs, cmp) => {
796796
fromArray(arr)
797797
}
798798

799-
let rec getByU = (xs, p) =>
799+
let rec findU = (xs, p) =>
800800
switch xs {
801801
| list{} => None
802802
| list{x, ...l} =>
803803
if p(. x) {
804804
Some(x)
805805
} else {
806-
getByU(l, p)
806+
findU(l, p)
807807
}
808808
}
809809

810-
let getBy = (xs, p) => getByU(xs, (. a) => p(a))
810+
let find = (xs, p) => findU(xs, (. a) => p(a))
811811

812812
let rec filterU = (xs, p) =>
813813
switch xs {

0 commit comments

Comments
 (0)