From 21d6c6e48e01afae44fff967097641c9a1ec295d Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sat, 17 Feb 2024 07:56:34 +0100 Subject: [PATCH 1/2] Align List api with other modules --- src/Core__List.mjs | 22 +++++++++++----------- src/Core__List.res | 32 ++++++++++++++++---------------- src/Core__List.resi | 34 +++++++++++++++++----------------- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/Core__List.mjs b/src/Core__List.mjs index bf07d0cf..dd1d1e0b 100644 --- a/src/Core__List.mjs +++ b/src/Core__List.mjs @@ -370,7 +370,7 @@ function copyAuxWithMapI(f, _i, _cellX, _prec) { return ; } var next = { - hd: f(i, cellX.hd), + hd: f(cellX.hd, i), tl: /* [] */0 }; prec.tl = next; @@ -541,14 +541,14 @@ function mapWithIndex(xs, f) { return /* [] */0; } var cell = { - hd: f(0, xs.hd), + hd: f(xs.hd, 0), tl: /* [] */0 }; copyAuxWithMapI(f, 1, xs.tl, cell); return cell; } -function makeBy(n, f) { +function fromInitializer(n, f) { if (n <= 0) { return /* [] */0; } @@ -672,7 +672,7 @@ function reverse(l) { return reverseConcat(l, /* [] */0); } -function flattenAux(_prec, _xs) { +function flatAux(_prec, _xs) { while(true) { var xs = _xs; var prec = _prec; @@ -686,7 +686,7 @@ function flattenAux(_prec, _xs) { }; } -function flatten(_xs) { +function flat(_xs) { while(true) { var xs = _xs; if (!xs) { @@ -698,7 +698,7 @@ function flatten(_xs) { hd: match.hd, tl: /* [] */0 }; - flattenAux(copyAuxCont(match.tl, cell), xs.tl); + flatAux(copyAuxCont(match.tl, cell), xs.tl); return cell; } _xs = xs.tl; @@ -761,7 +761,7 @@ function forEachWithIndex(l, f) { if (!xs) { return ; } - f(i, xs.hd); + f(xs.hd, i); _i = i + 1 | 0; _xs = xs.tl; continue ; @@ -1134,7 +1134,7 @@ function sort(xs, cmp) { return fromArray(arr); } -function getBy(_xs, p) { +function find(_xs, p) { while(true) { var xs = _xs; if (!xs) { @@ -1301,7 +1301,7 @@ export { get , getExn , make , - makeBy , + fromInitializer , toShuffled , drop , take , @@ -1309,7 +1309,7 @@ export { concat , concatMany , reverseConcat , - flatten , + flat , map , zip , zipBy , @@ -1335,7 +1335,7 @@ export { compare , equal , has , - getBy , + find , filter , filterWithIndex , filterMap , diff --git a/src/Core__List.res b/src/Core__List.res index e14bc492..b5cb4923 100644 --- a/src/Core__List.res +++ b/src/Core__List.res @@ -283,7 +283,7 @@ let rec copyAuxWithMap2 = (f, cellX, cellY, prec) => let rec copyAuxWithMapI = (f, i, cellX, prec) => switch cellX { | list{h, ...t} => - let next = mutableCell(f(i, h), list{}) + let next = mutableCell(f(h, i), list{}) unsafeMutateTail(prec, next) copyAuxWithMapI(f, i + 1, t, next) | list{} => () @@ -401,12 +401,12 @@ let mapWithIndex = (xs, f) => switch xs { | list{} => list{} | list{h, ...t} => - let cell = mutableCell(f(0, h), list{}) + let cell = mutableCell(f(h, 0), list{}) copyAuxWithMapI(f, 1, t, cell) cell } -let makeBy = (n, f) => +let fromInitializer = (~length as n, f) => if n <= 0 { list{} } else { @@ -423,7 +423,7 @@ let makeBy = (n, f) => headX } -let make = (type a, n, v: a): list => +let make = (type a, ~length as n, v: a): list => if n <= 0 { list{} } else { @@ -507,19 +507,19 @@ let rec reverseConcat = (l1, l2) => let reverse = l => reverseConcat(l, list{}) -let rec flattenAux = (prec, xs) => +let rec flatAux = (prec, xs) => switch xs { | list{} => unsafeMutateTail(prec, list{}) - | list{h, ...r} => flattenAux(copyAuxCont(h, prec), r) + | list{h, ...r} => flatAux(copyAuxCont(h, prec), r) } -let rec flatten = xs => +let rec flat = xs => switch xs { | list{} => list{} - | list{list{}, ...xs} => flatten(xs) + | list{list{}, ...xs} => flat(xs) | list{list{h, ...t}, ...r} => let cell = mutableCell(h, list{}) - flattenAux(copyAuxCont(t, cell), r) + flatAux(copyAuxCont(t, cell), r) cell } @@ -548,19 +548,19 @@ let rec forEach = (xs, f) => switch xs { | list{} => () | list{a, ...l} => - f(a)->ignore + f(a) forEach(l, f) } -let rec iteri = (xs, i, f) => +let rec forEachWithIndexAux = (xs, f, i) => switch xs { | list{} => () | list{a, ...l} => - f(i, a)->ignore - iteri(l, i + 1, f) + f(a, i) + forEachWithIndexAux(l, f, i + 1) } -let forEachWithIndex = (l, f) => iteri(l, 0, f) +let forEachWithIndex = (l, f) => forEachWithIndexAux(l, f, 0) let rec reduce = (l, accu, f) => switch l { @@ -751,14 +751,14 @@ let sort = (xs, cmp) => { fromArray(arr) } -let rec getBy = (xs, p) => +let rec find = (xs, p) => switch xs { | list{} => None | list{x, ...l} => if p(x) { Some(x) } else { - getBy(l, p) + find(l, p) } } diff --git a/src/Core__List.resi b/src/Core__List.resi index 5733ad17..d21ff025 100644 --- a/src/Core__List.resi +++ b/src/Core__List.resi @@ -173,10 +173,10 @@ with `value`. Returns an empty list if `value` is negative. ## Examples ```rescript -List.make(3, 1) // list{1, 1, 1} +List.make(~length=3, 1) // list{1, 1, 1} ``` */ -let make: (int, 'a) => t<'a> +let make: (~length: int, 'a) => t<'a> /** `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. ## Examples ```rescript -List.makeBy(5, i => i) // list{0, 1, 2, 3, 4} +List.fromInitializer(5, i => i) // list{0, 1, 2, 3, 4} -List.makeBy(5, i => i * i) // list{0, 1, 4, 9, 16} +List.fromInitializer(5, i => i * i) // list{0, 1, 4, 9, 16} ``` */ -let makeBy: (int, int => 'a) => t<'a> +let fromInitializer: (~length: int, int => 'a) => t<'a> /** `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} let reverseConcat: (t<'a>, t<'a>) => t<'a> /** -`flatten(list)` return the list obtained by concatenating all the lists in +`flat(list)` return the list obtained by concatenating all the lists in `list`, in order. ## Examples ```rescript -List.flatten(list{list{1, 2, 3}, list{}, list{3}}) // list{1, 2, 3, 3} +List.flat(list{list{1, 2, 3}, list{}, list{3}}) // list{1, 2, 3, 3} ``` */ -let flatten: t> => t<'a> +let flat: t> => t<'a> /** `map(list, f)` returns a new list with `f` applied to each element of `list`. @@ -337,10 +337,10 @@ that order. ## Examples ```rescript -list{1, 2, 3}->List.mapWithIndex((index, x) => index + x) // list{1, 3, 5} +list{1, 2, 3}->List.mapWithIndex((x, index) => index + x) // list{1, 3, 5} ``` */ -let mapWithIndex: (t<'a>, (int, 'a) => 'b) => t<'b> +let mapWithIndex: (t<'a>, ('a, int) => 'b) => t<'b> /** `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)) */ ``` */ -let forEach: (t<'a>, 'a => 'b) => unit +let forEach: (t<'a>, 'a => unit) => unit /** `forEachWithIndex(list, f, index)` call `f` on each element of `list` from beginning @@ -425,7 +425,7 @@ element from `list`. `f` returns `unit`. ## Examples ```rescript -List.forEachWithIndex(list{"a", "b", "c"}, (index, x) => { +List.forEachWithIndex(list{"a", "b", "c"}, (x, index) => { Console.log("Item " ++ Int.toString(index) ++ " is " ++ x) }) /* @@ -436,7 +436,7 @@ List.forEachWithIndex(list{"a", "b", "c"}, (index, x) => { */ ``` */ -let forEachWithIndex: (t<'a>, (int, 'a) => 'b) => unit +let forEachWithIndex: (t<'a>, ('a, int) => unit) => unit /** `reduce(list, initialValue, f)` applies `f` to each element of `list` from @@ -697,19 +697,19 @@ list{(-1), (-2), (-3)}->List.has(2, (a, b) => abs(a) == abs(b)) // true let has: (t<'a>, 'b, ('a, 'b) => bool) => bool /** -`getBy(list, f)` returns `Some(value)` for the first value in `list` that +`find(list, f)` returns `Some(value)` for the first value in `list` that satisfies the predicate function `f`. Returns `None` if no element satisfies the function. ## Examples ```rescript -List.getBy(list{1, 4, 3, 2}, x => x > 3) // Some(4) +List.find(list{1, 4, 3, 2}, x => x > 3) // Some(4) -List.getBy(list{1, 4, 3, 2}, x => x > 4) // None +List.find(list{1, 4, 3, 2}, x => x > 4) // None ``` */ -let getBy: (t<'a>, 'a => bool) => option<'a> +let find: (t<'a>, 'a => bool) => option<'a> /** `filter(list, f)` returns a list of all elements in `list` which satisfy the From 8ddcea13a4fc4fee784864aeeb6eb9fe730c180d Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Tue, 20 Feb 2024 10:08:48 +0100 Subject: [PATCH 2/2] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65ce313b..ccaa888c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next version +- Align List api with other modules (`List.getBy` -> `List.find` etc.). https://github.com/rescript-association/rescript-core/pull/195 - Add `Dict.forEach`, `Dict.forEachWithKey` and `Dict.mapValues` https://github.com/rescript-association/rescript-core/pull/181 - Remove internal xxxU helper functions that are not needed anymore in uncurried mode. https://github.com/rescript-association/rescript-core/pull/191 - Rename `Object.empty` to `Object.make` for consistency.