From a882beb83a7574abe28de10e437ba954b6792dd8 Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 18 Feb 2023 14:20:06 +0100 Subject: [PATCH 01/18] feat(array): add findMap --- src/Core__Array.mjs | 17 +++++++++++++++++ src/Core__Array.res | 14 ++++++++++++++ src/Core__Array.resi | 3 ++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index f675dff5..6de9103f 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -112,6 +112,22 @@ function flatMap(a, f) { return Caml_splice_call.spliceObjApply([], "concat", [a.map(f)]); } +function findMap(arr, f) { + var _i = 0; + while(true) { + var i = _i; + if (i === arr.length) { + return ; + } + var r = Curry._1(f, arr[i]); + if (r !== undefined) { + return r; + } + _i = i + 1 | 0; + continue ; + }; +} + export { sort , indexOfOpt , @@ -125,5 +141,6 @@ export { shuffle , shuffleInPlace , flatMap , + findMap , } /* No side effect */ diff --git a/src/Core__Array.res b/src/Core__Array.res index 658f45c8..c2b3db18 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -203,4 +203,18 @@ let filterMap = (a, f) => filterMapU(a, (. a) => f(a)) // TODO: Change this implementation? let flatMap = (a, f) => []->concatMany(map(a, f)) +let findMap = (arr, f) => { + let rec loop = i => + if i == arr->length { + None + } else { + switch f(getUnsafe(arr, i)) { + | None => loop(i + 1) + | Some(_) as r => r + } + } + + loop(0) +} + @send external at: (array<'a>, int) => option<'a> = "at" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 562f739d..71030b0e 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -104,8 +104,9 @@ let filterMap: (array<'a>, 'a => option<'b>) => array<'b> let shuffle: array<'a> => array<'a> let shuffleInPlace: array<'a> => unit let flatMap: (array<'a>, 'a => array<'b>) => array<'b> +let findMap: (array<'a>, 'a => option<'b>) => option<'b> -/** +/** `at(array, index)` Get an element by its index. Negative indices count backwards from the last item. From 31c8f64f6f8c3b65648746a3d1a31a436d6011e5 Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 18 Feb 2023 15:01:55 +0100 Subject: [PATCH 02/18] feat(array): add init --- src/Core__Array.mjs | 12 ++++++++++++ src/Core__Array.res | 15 +++++++++++++-- src/Core__Array.resi | 1 + 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index 6de9103f..8383af8a 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -5,6 +5,17 @@ import * as Js_math from "rescript/lib/es6/js_math.js"; import * as Caml_option from "rescript/lib/es6/caml_option.js"; import * as Caml_splice_call from "rescript/lib/es6/caml_splice_call.js"; +function init(len, f) { + if (len <= 0) { + return []; + } + var arr = new Array(len); + for(var i = 0; i < len; ++i){ + arr[i] = Curry._1(f, i); + } + return arr; +} + function indexOfOpt(arr, item) { var index = arr.indexOf(item); if (index !== -1) { @@ -129,6 +140,7 @@ function findMap(arr, f) { } export { + init , sort , indexOfOpt , lastIndexOfOpt , diff --git a/src/Core__Array.res b/src/Core__Array.res index c2b3db18..5ed477e6 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -1,3 +1,5 @@ +@new external makeUninitializedUnsafe: int => array<'a> = "Array" +@set external truncateToLengthUnsafe: (array<'a>, int) => unit = "length" external getUnsafe: (array<'a>, int) => 'a = "%array_unsafe_get" external setUnsafe: (array<'a>, int, 'a) => unit = "%array_unsafe_set" @@ -11,6 +13,17 @@ external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> @val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from" @val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from" +let init = (len, f) => + if len <= 0 { + [] + } else { + let arr = makeUninitializedUnsafe(len) + for i in 0 to len - 1 { + arr->setUnsafe(i, f(i)) + } + arr + } + @val external isArray: 'a => bool = "Array.isArray" @get external length: array<'a> => int = "length" @@ -151,8 +164,6 @@ let findIndexOpt = (array: array<'a>, finder: 'a => bool): option => | index => Some(index) } -@new external makeUninitializedUnsafe: int => array<'a> = "Array" -@set external truncateToLengthUnsafe: (array<'a>, int) => unit = "length" let swapUnsafe = (xs, i, j) => { let tmp = getUnsafe(xs, i) setUnsafe(xs, i, getUnsafe(xs, j)) diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 71030b0e..1d991537 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -5,6 +5,7 @@ external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> = "Array.from" @val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from" @val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from" +let init: (int, int => 'a) => array<'a> @val external isArray: 'a => bool = "Array.isArray" @get external length: array<'a> => int = "length" @send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin" From 307da8955fee223ae55fd37befda7ee02be3ff1f Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 18 Feb 2023 15:04:37 +0100 Subject: [PATCH 03/18] feat(array): add make --- src/Core__Array.mjs | 12 ++++++++++++ src/Core__Array.res | 11 +++++++++++ src/Core__Array.resi | 1 + 3 files changed, 24 insertions(+) diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index 8383af8a..65376042 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -5,6 +5,17 @@ import * as Js_math from "rescript/lib/es6/js_math.js"; import * as Caml_option from "rescript/lib/es6/caml_option.js"; import * as Caml_splice_call from "rescript/lib/es6/caml_splice_call.js"; +function make(len, x) { + if (len <= 0) { + return []; + } + var arr = new Array(len); + for(var i = 0; i < len; ++i){ + arr[i] = x; + } + return arr; +} + function init(len, f) { if (len <= 0) { return []; @@ -140,6 +151,7 @@ function findMap(arr, f) { } export { + make , init , sort , indexOfOpt , diff --git a/src/Core__Array.res b/src/Core__Array.res index 5ed477e6..e32adddf 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -13,6 +13,17 @@ external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> @val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from" @val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from" +let make = (len, x) => + if len <= 0 { + [] + } else { + let arr = makeUninitializedUnsafe(len) + for i in 0 to len - 1 { + arr->setUnsafe(i, x) + } + arr + } + let init = (len, f) => if len <= 0 { [] diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 1d991537..00f581c0 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -5,6 +5,7 @@ external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> = "Array.from" @val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from" @val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from" +let make: (int, 'a) => array<'a> let init: (int, int => 'a) => array<'a> @val external isArray: 'a => bool = "Array.isArray" @get external length: array<'a> => int = "length" From 12402ae2b4df64c2cf92ed142ccf78e2609cd889 Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 18 Feb 2023 15:15:25 +0100 Subject: [PATCH 04/18] feat(array): add keepSome --- src/Core__Array.mjs | 7 +++++++ src/Core__Array.res | 2 ++ src/Core__Array.resi | 1 + 3 files changed, 10 insertions(+) diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index 65376042..951e01bf 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -130,6 +130,12 @@ function filterMap(a, f) { return r; } +function keepSome(__x) { + return filterMap(__x, (function (x) { + return x; + })); +} + function flatMap(a, f) { return Caml_splice_call.spliceObjApply([], "concat", [a.map(f)]); } @@ -162,6 +168,7 @@ export { findIndexOpt , reverse , filterMap , + keepSome , shuffle , shuffleInPlace , flatMap , diff --git a/src/Core__Array.res b/src/Core__Array.res index e32adddf..c4ca2777 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -222,6 +222,8 @@ let filterMapU = (a, f) => { let filterMap = (a, f) => filterMapU(a, (. a) => f(a)) +let keepSome = filterMap(_, x => x) + // TODO: Change this implementation? let flatMap = (a, f) => []->concatMany(map(a, f)) diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 00f581c0..909a43d8 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -103,6 +103,7 @@ external setUnsafe: (array<'a>, int, 'a) => unit = "%array_unsafe_set" let findIndexOpt: (array<'a>, 'a => bool) => option let reverse: array<'a> => array<'a> let filterMap: (array<'a>, 'a => option<'b>) => array<'b> +let keepSome: array> => array<'a> let shuffle: array<'a> => array<'a> let shuffleInPlace: array<'a> => unit let flatMap: (array<'a>, 'a => array<'b>) => array<'b> From 2370ea64aa3f17a9fcbdc051fe49d7090c13cd10 Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 18 Feb 2023 16:33:02 +0100 Subject: [PATCH 05/18] refactor(array): bind to flatMap instead of reimplementing it --- src/Core__Array.mjs | 6 ------ src/Core__Array.res | 3 +-- src/Core__Array.resi | 2 +- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index 951e01bf..052551be 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -3,7 +3,6 @@ import * as Curry from "rescript/lib/es6/curry.js"; import * as Js_math from "rescript/lib/es6/js_math.js"; import * as Caml_option from "rescript/lib/es6/caml_option.js"; -import * as Caml_splice_call from "rescript/lib/es6/caml_splice_call.js"; function make(len, x) { if (len <= 0) { @@ -136,10 +135,6 @@ function keepSome(__x) { })); } -function flatMap(a, f) { - return Caml_splice_call.spliceObjApply([], "concat", [a.map(f)]); -} - function findMap(arr, f) { var _i = 0; while(true) { @@ -171,7 +166,6 @@ export { keepSome , shuffle , shuffleInPlace , - flatMap , findMap , } /* No side effect */ diff --git a/src/Core__Array.res b/src/Core__Array.res index c4ca2777..d45d9056 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -224,8 +224,7 @@ let filterMap = (a, f) => filterMapU(a, (. a) => f(a)) let keepSome = filterMap(_, x => x) -// TODO: Change this implementation? -let flatMap = (a, f) => []->concatMany(map(a, f)) +@send external flatMap: (array<'a>, 'a => array<'b>) => array<'b> = "flatMap" let findMap = (arr, f) => { let rec loop = i => diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 909a43d8..4456ecd9 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -106,7 +106,7 @@ let filterMap: (array<'a>, 'a => option<'b>) => array<'b> let keepSome: array> => array<'a> let shuffle: array<'a> => array<'a> let shuffleInPlace: array<'a> => unit -let flatMap: (array<'a>, 'a => array<'b>) => array<'b> +@send external flatMap: (array<'a>, 'a => array<'b>) => array<'b> = "flatMap" let findMap: (array<'a>, 'a => option<'b>) => option<'b> /** From aa0607865840a6a53e75fd175d687353dad2cc2e Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 18 Feb 2023 21:45:35 +0100 Subject: [PATCH 06/18] refactor(array/make): use fillAllInPlace instead of manual loop --- src/Core__Array.mjs | 4 +--- src/Core__Array.res | 16 +++++++--------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index 052551be..169799c3 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -9,9 +9,7 @@ function make(len, x) { return []; } var arr = new Array(len); - for(var i = 0; i < len; ++i){ - arr[i] = x; - } + arr.fill(x); return arr; } diff --git a/src/Core__Array.res b/src/Core__Array.res index d45d9056..b8047d1f 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -13,14 +13,18 @@ external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> @val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from" @val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from" +@send external fillAllInPlace: (array<'a>, 'a) => unit = "fill" + +@send external fillInPlaceToEnd: (array<'a>, 'a, ~start: int) => unit = "fill" + +@send external fillInPlace: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill" + let make = (len, x) => if len <= 0 { [] } else { let arr = makeUninitializedUnsafe(len) - for i in 0 to len - 1 { - arr->setUnsafe(i, x) - } + arr->fillAllInPlace(x) arr } @@ -47,12 +51,6 @@ external copyWithinToEnd: (array<'a>, ~target: int, ~start: int) => array<'a> = @send external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<'a> = "copyWithin" -@send external fillAllInPlace: (array<'a>, 'a) => unit = "fill" - -@send external fillInPlaceToEnd: (array<'a>, 'a, ~start: int) => unit = "fill" - -@send external fillInPlace: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill" - @send external pop: array<'a> => option<'a> = "pop" @send external push: (array<'a>, 'a) => unit = "push" From e02724ce7c31b21ab219f00ec4bee81a5f0323ae Mon Sep 17 00:00:00 2001 From: glennsl Date: Sun, 19 Feb 2023 20:13:37 +0100 Subject: [PATCH 07/18] feat(array): use labelled argument for length in make and init --- src/Core__Array.mjs | 14 +++++++------- src/Core__Array.res | 14 +++++++------- src/Core__Array.resi | 4 ++-- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index 169799c3..741b1a79 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -4,21 +4,21 @@ import * as Curry from "rescript/lib/es6/curry.js"; import * as Js_math from "rescript/lib/es6/js_math.js"; import * as Caml_option from "rescript/lib/es6/caml_option.js"; -function make(len, x) { - if (len <= 0) { +function make(length, x) { + if (length <= 0) { return []; } - var arr = new Array(len); + var arr = new Array(length); arr.fill(x); return arr; } -function init(len, f) { - if (len <= 0) { +function init(length, f) { + if (length <= 0) { return []; } - var arr = new Array(len); - for(var i = 0; i < len; ++i){ + var arr = new Array(length); + for(var i = 0; i < length; ++i){ arr[i] = Curry._1(f, i); } return arr; diff --git a/src/Core__Array.res b/src/Core__Array.res index b8047d1f..8fb68189 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -19,21 +19,21 @@ external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> @send external fillInPlace: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill" -let make = (len, x) => - if len <= 0 { +let make = (~length, x) => + if length <= 0 { [] } else { - let arr = makeUninitializedUnsafe(len) + let arr = makeUninitializedUnsafe(length) arr->fillAllInPlace(x) arr } -let init = (len, f) => - if len <= 0 { +let init = (~length, f) => + if length <= 0 { [] } else { - let arr = makeUninitializedUnsafe(len) - for i in 0 to len - 1 { + let arr = makeUninitializedUnsafe(length) + for i in 0 to length - 1 { arr->setUnsafe(i, f(i)) } arr diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 4456ecd9..dac621aa 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -5,8 +5,8 @@ external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> = "Array.from" @val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from" @val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from" -let make: (int, 'a) => array<'a> -let init: (int, int => 'a) => array<'a> +let make: (~length: int, 'a) => array<'a> +let init: (~length: int, int => 'a) => array<'a> @val external isArray: 'a => bool = "Array.isArray" @get external length: array<'a> => int = "length" @send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin" From c8a67b5c5111adc5599c5f4acfff5b862296d237 Mon Sep 17 00:00:00 2001 From: glennsl Date: Sun, 19 Feb 2023 20:15:04 +0100 Subject: [PATCH 08/18] refactor(array): rename init -> fromInitializer --- src/Core__Array.mjs | 4 ++-- src/Core__Array.res | 2 +- src/Core__Array.resi | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index 741b1a79..debbd163 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -13,7 +13,7 @@ function make(length, x) { return arr; } -function init(length, f) { +function fromInitializer(length, f) { if (length <= 0) { return []; } @@ -151,7 +151,7 @@ function findMap(arr, f) { export { make , - init , + fromInitializer , sort , indexOfOpt , lastIndexOfOpt , diff --git a/src/Core__Array.res b/src/Core__Array.res index 8fb68189..77c3297d 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -28,7 +28,7 @@ let make = (~length, x) => arr } -let init = (~length, f) => +let fromInitializer = (~length, f) => if length <= 0 { [] } else { diff --git a/src/Core__Array.resi b/src/Core__Array.resi index dac621aa..11457d5f 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -6,7 +6,7 @@ external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> @val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from" @val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from" let make: (~length: int, 'a) => array<'a> -let init: (~length: int, int => 'a) => array<'a> +let fromInitializer: (~length: int, int => 'a) => array<'a> @val external isArray: 'a => bool = "Array.isArray" @get external length: array<'a> => int = "length" @send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin" From 93110d08c642c3791f41fe3b8ed898b7c4f3bfd3 Mon Sep 17 00:00:00 2001 From: glennsl Date: Sun, 26 Feb 2023 15:11:19 +0100 Subject: [PATCH 09/18] test(array): add tests for non-native functions --- package.json | 6 +- test/ArrayTests.mjs | 321 ++++++++++++++++++++++++++++++++++++++++++++ test/ArrayTests.res | 74 ++++++++++ test/TestSuite.mjs | 35 +++++ test/TestSuite.res | 2 + 5 files changed, 434 insertions(+), 4 deletions(-) create mode 100644 test/ArrayTests.mjs create mode 100644 test/ArrayTests.res create mode 100644 test/TestSuite.mjs create mode 100644 test/TestSuite.res diff --git a/package.json b/package.json index d23f0f91..f4a3fb7b 100644 --- a/package.json +++ b/package.json @@ -5,11 +5,9 @@ "clean": "rescript clean", "build": "rescript", "watch": "rescript build -w", - "test": "node test/PromiseTest.mjs && node test/TempTests.mjs" + "test": "node test/TestSuite.mjs && node test/TempTests.mjs" }, - "keywords": [ - "rescript" - ], + "keywords": ["rescript"], "homepage": "https://github.com/rescript-association/rescript-core", "author": "ReScript Team", "license": "MIT", diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs new file mode 100644 index 00000000..bc52af57 --- /dev/null +++ b/test/ArrayTests.mjs @@ -0,0 +1,321 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Test from "./Test.mjs"; +import * as Caml_obj from "rescript/lib/es6/caml_obj.js"; +import * as Core__List from "../src/Core__List.mjs"; +import * as Core__Array from "../src/Core__Array.mjs"; + +var eq = Caml_obj.equal; + +Test.run([ + [ + "ArrayTests.res", + 5, + 20, + 26 + ], + "make" + ], Core__Array.make(6, 7), eq, [ + 7, + 7, + 7, + 7, + 7, + 7 + ]); + +Test.run([ + [ + "ArrayTests.res", + 8, + 13, + 30 + ], + "fromInitializer" + ], Core__Array.fromInitializer(7, (function (i) { + return i + 3 | 0; + })), eq, [ + 3, + 4, + 5, + 6, + 7, + 8, + 9 + ]); + +Test.run([ + [ + "ArrayTests.res", + 14, + 20, + 28 + ], + "reduce" + ], Core__Array.reduce([ + 1, + 2, + 3 + ], /* [] */0, Core__List.add), eq, { + hd: 3, + tl: { + hd: 2, + tl: { + hd: 1, + tl: /* [] */0 + } + } + }); + +Test.run([ + [ + "ArrayTests.res", + 15, + 20, + 36 + ], + "reduce - empty" + ], Core__Array.reduce([], /* [] */0, Core__List.add), eq, /* [] */0); + +Test.run([ + [ + "ArrayTests.res", + 18, + 13, + 28 + ], + "reduceReverse" + ], Core__Array.reduceReverse([ + 1, + 2, + 3 + ], /* [] */0, Core__List.add), eq, { + hd: 1, + tl: { + hd: 2, + tl: { + hd: 3, + tl: /* [] */0 + } + } + }); + +Test.run([ + [ + "ArrayTests.res", + 23, + 20, + 43 + ], + "reduceReverse - empty" + ], Core__Array.reduceReverse([], /* [] */0, Core__List.add), eq, /* [] */0); + +Test.run([ + [ + "ArrayTests.res", + 25, + 20, + 38 + ], + "shuffle - length" + ], Core__Array.shuffle([ + 1, + 2, + 3 + ]).length, eq, 3); + +var arr = [ + 1, + 2, + 3 +]; + +Test.run([ + [ + "ArrayTests.res", + 28, + 13, + 38 + ], + "shuffleInPlace - length" + ], (Core__Array.shuffleInPlace(arr), arr.length), eq, 3); + +Test.run([ + [ + "ArrayTests.res", + 39, + 13, + 24 + ], + "filterMap" + ], Core__Array.filterMap([ + 1, + 2, + 3, + 4, + 5, + 6 + ], (function (n) { + if (n % 2 === 0) { + return Math.imul(n, n); + } + + })), eq, [ + 4, + 16, + 36 + ]); + +Test.run([ + [ + "ArrayTests.res", + 44, + 20, + 42 + ], + "filterMap - no match" + ], Core__Array.filterMap([ + 1, + 2, + 3, + 4, + 5, + 6 + ], (function (param) { + + })), eq, []); + +Test.run([ + [ + "ArrayTests.res", + 46, + 13, + 32 + ], + "filterMap - empty" + ], Core__Array.filterMap([], (function (n) { + if (n % 2 === 0) { + return Math.imul(n, n); + } + + })), eq, []); + +Test.run([ + [ + "ArrayTests.res", + 52, + 20, + 30 + ], + "keepSome" + ], Core__Array.keepSome([ + 1, + undefined, + 3 + ]), eq, [ + 1, + 3 + ]); + +Test.run([ + [ + "ArrayTests.res", + 54, + 13, + 34 + ], + "keepSome - all Some" + ], Core__Array.keepSome([ + 1, + 2, + 3 + ]), eq, [ + 1, + 2, + 3 + ]); + +Test.run([ + [ + "ArrayTests.res", + 59, + 20, + 41 + ], + "keepSome - all None" + ], Core__Array.keepSome([ + undefined, + undefined, + undefined + ]), eq, []); + +Test.run([ + [ + "ArrayTests.res", + 60, + 20, + 38 + ], + "keepSome - empty" + ], Core__Array.keepSome([]), eq, []); + +Test.run([ + [ + "ArrayTests.res", + 63, + 13, + 22 + ], + "findMap" + ], Core__Array.findMap([ + 1, + 2, + 3, + 4, + 5, + 6 + ], (function (n) { + if (n % 2 === 0) { + return n - 8 | 0; + } + + })), eq, -6); + +Test.run([ + [ + "ArrayTests.res", + 68, + 20, + 40 + ], + "findMap - no match" + ], Core__Array.findMap([ + 1, + 2, + 3, + 4, + 5, + 6 + ], (function (param) { + + })), eq, undefined); + +Test.run([ + [ + "ArrayTests.res", + 70, + 13, + 30 + ], + "findMap - empty" + ], Core__Array.findMap([], (function (n) { + if (n % 2 === 0) { + return Math.imul(n, n); + } + + })), eq, undefined); + +export { + eq , +} +/* Not a pure module */ diff --git a/test/ArrayTests.res b/test/ArrayTests.res new file mode 100644 index 00000000..7148fd08 --- /dev/null +++ b/test/ArrayTests.res @@ -0,0 +1,74 @@ +open RescriptCore + +let eq = (a, b) => a == b + +Test.run(__POS_OF__("make"), Array.make(~length=6, 7), eq, [7, 7, 7, 7, 7, 7]) + +Test.run( + __POS_OF__("fromInitializer"), + Array.fromInitializer(~length=7, i => i + 3), + eq, + [3, 4, 5, 6, 7, 8, 9], +) + +Test.run(__POS_OF__("reduce"), Array.reduce([1, 2, 3], list{}, List.add), eq, list{3, 2, 1}) +Test.run(__POS_OF__("reduce - empty"), Array.reduce([], list{}, List.add), eq, list{}) + +Test.run( + __POS_OF__("reduceReverse"), + Array.reduceReverse([1, 2, 3], list{}, List.add), + eq, + list{1, 2, 3}, +) +Test.run(__POS_OF__("reduceReverse - empty"), Array.reduceReverse([], list{}, List.add), eq, list{}) + +Test.run(__POS_OF__("shuffle - length"), Array.shuffle([1, 2, 3])->Array.length, eq, 3) + +Test.run( + __POS_OF__("shuffleInPlace - length"), + { + let arr = [1, 2, 3] + Array.shuffleInPlace(arr) + arr->Array.length + }, + eq, + 3, +) + +Test.run( + __POS_OF__("filterMap"), + Array.filterMap([1, 2, 3, 4, 5, 6], n => mod(n, 2) == 0 ? Some(n * n) : None), + eq, + [4, 16, 36], +) +Test.run(__POS_OF__("filterMap - no match"), Array.filterMap([1, 2, 3, 4, 5, 6], _ => None), eq, []) +Test.run( + __POS_OF__("filterMap - empty"), + Array.filterMap([], n => mod(n, 2) == 0 ? Some(n * n) : None), + eq, + [], +) + +Test.run(__POS_OF__("keepSome"), Array.keepSome([Some(1), None, Some(3)]), eq, [1, 3]) +Test.run( + __POS_OF__("keepSome - all Some"), + Array.keepSome([Some(1), Some(2), Some(3)]), + eq, + [1, 2, 3], +) +Test.run(__POS_OF__("keepSome - all None"), Array.keepSome([None, None, None]), eq, []) +Test.run(__POS_OF__("keepSome - empty"), Array.keepSome([]), eq, []) + +Test.run( + __POS_OF__("findMap"), + Array.findMap([1, 2, 3, 4, 5, 6], n => mod(n, 2) == 0 ? Some(n - 8) : None), + eq, + Some(-6), +) +Test.run(__POS_OF__("findMap - no match"), Array.findMap([1, 2, 3, 4, 5, 6], _ => None), eq, None) +Test.run( + __POS_OF__("findMap - empty"), + Array.findMap([], n => mod(n, 2) == 0 ? Some(n * n) : None), + eq, + None, +) diff --git a/test/TestSuite.mjs b/test/TestSuite.mjs new file mode 100644 index 00000000..59a2f4e7 --- /dev/null +++ b/test/TestSuite.mjs @@ -0,0 +1,35 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as ArrayTests from "./ArrayTests.mjs"; +import * as PromiseTest from "./PromiseTest.mjs"; + +var TestError = PromiseTest.TestError; + +var fail = PromiseTest.fail; + +var equal = PromiseTest.equal; + +var Creation = PromiseTest.Creation; + +var ThenChaining = PromiseTest.ThenChaining; + +var Rejection = PromiseTest.Rejection; + +var Catching = PromiseTest.Catching; + +var Concurrently = PromiseTest.Concurrently; + +var eq = ArrayTests.eq; + +export { + TestError , + fail , + equal , + Creation , + ThenChaining , + Rejection , + Catching , + Concurrently , + eq , +} +/* ArrayTests Not a pure module */ diff --git a/test/TestSuite.res b/test/TestSuite.res new file mode 100644 index 00000000..a376fea1 --- /dev/null +++ b/test/TestSuite.res @@ -0,0 +1,2 @@ +include PromiseTest +include ArrayTests From 0090ab63926ac236e26e2bdeeffff911b8e8997d Mon Sep 17 00:00:00 2001 From: glennsl Date: Sun, 26 Feb 2023 15:17:33 +0100 Subject: [PATCH 10/18] refactor(array): bind to native reduce instead of reimplementing it --- src/Core__Array.mjs | 10 ---------- src/Core__Array.res | 10 +--------- src/Core__Array.resi | 9 +++++---- test/ArrayTests.mjs | 12 ++++++------ test/ArrayTests.res | 4 ++-- test/TempTests.mjs | 9 ++++----- test/TempTests.res | 2 +- 7 files changed, 19 insertions(+), 37 deletions(-) diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index debbd163..63e7bbaf 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -46,15 +46,6 @@ function sort(arr, cmp) { return result; } -function reduce(a, x, f) { - var f$1 = Curry.__2(f); - var r = x; - for(var i = 0 ,i_finish = a.length; i < i_finish; ++i){ - r = f$1(r, a[i]); - } - return r; -} - function reduceWithIndex(a, x, f) { var f$1 = Curry.__3(f); var r = x; @@ -155,7 +146,6 @@ export { sort , indexOfOpt , lastIndexOfOpt , - reduce , reduceReverse , reduceWithIndex , findIndexOpt , diff --git a/src/Core__Array.res b/src/Core__Array.res index 77c3297d..188ea8fb 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -127,15 +127,7 @@ let sort = (arr, cmp) => { @send external map: (array<'a>, 'a => 'b) => array<'b> = "map" @send external mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b> = "map" -let reduceU = (a, x, f) => { - let r = ref(x) - for i in 0 to length(a) - 1 { - r.contents = f(. r.contents, getUnsafe(a, i)) - } - r.contents -} - -let reduce = (a, x, f) => reduceU(a, x, (. a, b) => f(a, b)) +@send external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce" let reduceWithIndexU = (a, x, f) => { let r = ref(x) diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 11457d5f..f411b361 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -59,17 +59,18 @@ let lastIndexOfOpt: (array<'a>, 'a) => option @send external mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b> = "map" /** - `reduce(xs, init, f)` + `reduce(xs, f, init)` Applies `f` to each element of `xs` from beginning to end. Function `f` has two parameters: the item from the list and an “accumulator”; which starts with a value of `init`. `reduce` returns the final value of the accumulator. ```res example - Array.reduce([2, 3, 4], 1, (a, b) => a + b) == 10 + Array.reduce([2, 3, 4], (a, b) => a + b, 1) == 10 - Array.reduce(["a", "b", "c", "d"], "", (a, b) => a ++ b) == "abcd" + Array.reduce(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "abcd" ``` */ -let reduce: (array<'b>, 'a, ('a, 'b) => 'a) => 'a +@send +external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce" /** `reduceReverse(xs, init, f)` diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs index bc52af57..0792d28c 100644 --- a/test/ArrayTests.mjs +++ b/test/ArrayTests.mjs @@ -52,11 +52,11 @@ Test.run([ 28 ], "reduce" - ], Core__Array.reduce([ - 1, - 2, - 3 - ], /* [] */0, Core__List.add), eq, { + ], [ + 1, + 2, + 3 + ].reduce(Core__List.add, /* [] */0), eq, { hd: 3, tl: { hd: 2, @@ -75,7 +75,7 @@ Test.run([ 36 ], "reduce - empty" - ], Core__Array.reduce([], /* [] */0, Core__List.add), eq, /* [] */0); + ], [].reduce(Core__List.add, /* [] */0), eq, /* [] */0); Test.run([ [ diff --git a/test/ArrayTests.res b/test/ArrayTests.res index 7148fd08..ec08c4ea 100644 --- a/test/ArrayTests.res +++ b/test/ArrayTests.res @@ -11,8 +11,8 @@ Test.run( [3, 4, 5, 6, 7, 8, 9], ) -Test.run(__POS_OF__("reduce"), Array.reduce([1, 2, 3], list{}, List.add), eq, list{3, 2, 1}) -Test.run(__POS_OF__("reduce - empty"), Array.reduce([], list{}, List.add), eq, list{}) +Test.run(__POS_OF__("reduce"), Array.reduce([1, 2, 3], List.add, list{}), eq, list{3, 2, 1}) +Test.run(__POS_OF__("reduce - empty"), Array.reduce([], List.add, list{}), eq, list{}) Test.run( __POS_OF__("reduceReverse"), diff --git a/test/TempTests.mjs b/test/TempTests.mjs index 9807a51a..cfd7146b 100644 --- a/test/TempTests.mjs +++ b/test/TempTests.mjs @@ -4,7 +4,6 @@ import * as Core__Int from "../src/Core__Int.mjs"; import * as Core__Dict from "../src/Core__Dict.mjs"; import * as Core__JSON from "../src/Core__JSON.mjs"; import * as Caml_option from "rescript/lib/es6/caml_option.js"; -import * as Core__Array from "../src/Core__Array.mjs"; import * as Core__Float from "../src/Core__Float.mjs"; import * as Core__BigInt from "../src/Core__BigInt.mjs"; import * as Core__Option from "../src/Core__Option.mjs"; @@ -22,11 +21,11 @@ var array = [ 4 ]; -console.info(Core__Array.reduce(array.map(function (x) { - return (x << 1); - }), 0, (function (a, b) { +console.info(array.map(function (x) { + return (x << 1); + }).reduce((function (a, b) { return a + b | 0; - }))); + }), 0)); console.info(typeof array); diff --git a/test/TempTests.res b/test/TempTests.res index 82784cea..9b7d5a6b 100644 --- a/test/TempTests.res +++ b/test/TempTests.res @@ -4,7 +4,7 @@ Console.info("") Console.info("Array") Console.info("---") let array = [1, 2, 3, 4] -Console.info(array->Array.map(x => x * 2)->Array.reduce(0, (a, b) => a + b)) +Console.info(array->Array.map(x => x * 2)->Array.reduce((a, b) => a + b, 0)) Console.info(typeof(array)) Console.info("") From a2e2df9f3d0ace63785f5a7bd029f7d19c7d66fd Mon Sep 17 00:00:00 2001 From: glennsl Date: Sun, 26 Feb 2023 15:21:08 +0100 Subject: [PATCH 11/18] refactor(array): replace reduceReverse with native binding to reduceRight --- src/Core__Array.mjs | 10 ---------- src/Core__Array.res | 11 ++--------- src/Core__Array.resi | 7 ++++--- test/ArrayTests.mjs | 20 ++++++++++---------- test/ArrayTests.res | 6 +++--- 5 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index 63e7bbaf..446f0c72 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -55,15 +55,6 @@ function reduceWithIndex(a, x, f) { return r; } -function reduceReverse(a, x, f) { - var f$1 = Curry.__2(f); - var r = x; - for(var i = a.length - 1 | 0; i >= 0; --i){ - r = f$1(r, a[i]); - } - return r; -} - function findIndexOpt(array, finder) { var index = array.findIndex(finder); if (index !== -1) { @@ -146,7 +137,6 @@ export { sort , indexOfOpt , lastIndexOfOpt , - reduceReverse , reduceWithIndex , findIndexOpt , reverse , diff --git a/src/Core__Array.res b/src/Core__Array.res index 188ea8fb..aecb7d5d 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -139,15 +139,8 @@ let reduceWithIndexU = (a, x, f) => { let reduceWithIndex = (a, x, f) => reduceWithIndexU(a, x, (. a, b, c) => f(a, b, c)) -let reduceReverseU = (a, x, f) => { - let r = ref(x) - for i in length(a) - 1 downto 0 { - r.contents = f(. r.contents, getUnsafe(a, i)) - } - r.contents -} - -let reduceReverse = (a, x, f) => reduceReverseU(a, x, (. a, b) => f(a, b)) +@send +external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight" @send external some: (array<'a>, 'a => bool) => bool = "some" @send external someWithIndex: (array<'a>, ('a, int) => bool) => bool = "some" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index f411b361..505997ae 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -73,15 +73,16 @@ let lastIndexOfOpt: (array<'a>, 'a) => option external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce" /** - `reduceReverse(xs, init, f)` + `reduceRight(xs, f, init)` Works like `Array.reduce`; except that function `f` is applied to each item of `xs` from the last back to the first. ```res example - Array.reduceReverse(["a", "b", "c", "d"], "", (a, b) => a ++ b) == "dcba" + Array.reduceRight(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "dcba" ``` */ -let reduceReverse: (array<'b>, 'a, ('a, 'b) => 'a) => 'a +@send +external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight" /** Applies `f` to each element of `xs` from beginning to end. Function `f` has three parameters: the item from the array and an “accumulator”, which starts with a value of `init` and the index of each element. `reduceWithIndex` returns the final value of the accumulator. diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs index 0792d28c..10c4c863 100644 --- a/test/ArrayTests.mjs +++ b/test/ArrayTests.mjs @@ -82,14 +82,14 @@ Test.run([ "ArrayTests.res", 18, 13, - 28 + 26 ], - "reduceReverse" - ], Core__Array.reduceReverse([ - 1, - 2, - 3 - ], /* [] */0, Core__List.add), eq, { + "reduceRight" + ], [ + 1, + 2, + 3 + ].reduceRight(Core__List.add, /* [] */0), eq, { hd: 1, tl: { hd: 2, @@ -105,10 +105,10 @@ Test.run([ "ArrayTests.res", 23, 20, - 43 + 41 ], - "reduceReverse - empty" - ], Core__Array.reduceReverse([], /* [] */0, Core__List.add), eq, /* [] */0); + "reduceRight - empty" + ], [].reduceRight(Core__List.add, /* [] */0), eq, /* [] */0); Test.run([ [ diff --git a/test/ArrayTests.res b/test/ArrayTests.res index ec08c4ea..d5610803 100644 --- a/test/ArrayTests.res +++ b/test/ArrayTests.res @@ -15,12 +15,12 @@ Test.run(__POS_OF__("reduce"), Array.reduce([1, 2, 3], List.add, list{}), eq, li Test.run(__POS_OF__("reduce - empty"), Array.reduce([], List.add, list{}), eq, list{}) Test.run( - __POS_OF__("reduceReverse"), - Array.reduceReverse([1, 2, 3], list{}, List.add), + __POS_OF__("reduceRight"), + Array.reduceRight([1, 2, 3], List.add, list{}), eq, list{1, 2, 3}, ) -Test.run(__POS_OF__("reduceReverse - empty"), Array.reduceReverse([], list{}, List.add), eq, list{}) +Test.run(__POS_OF__("reduceRight - empty"), Array.reduceRight([], List.add, list{}), eq, list{}) Test.run(__POS_OF__("shuffle - length"), Array.shuffle([1, 2, 3])->Array.length, eq, 3) From 3042949160d207057f33ffa87ab236a6b44a15f6 Mon Sep 17 00:00:00 2001 From: glennsl Date: Sun, 26 Feb 2023 15:25:22 +0100 Subject: [PATCH 12/18] refactor(array): use native reduceWithIndex instead of reimplementing it --- src/Core__Array.mjs | 10 ------- src/Core__Array.res | 12 +------- src/Core__Array.resi | 17 ++++++----- test/ArrayTests.mjs | 69 +++++++++++++++++++++++++++++++++++--------- test/ArrayTests.res | 13 +++++++++ 5 files changed, 80 insertions(+), 41 deletions(-) diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index 446f0c72..a9fea805 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -46,15 +46,6 @@ function sort(arr, cmp) { return result; } -function reduceWithIndex(a, x, f) { - var f$1 = Curry.__3(f); - var r = x; - for(var i = 0 ,i_finish = a.length; i < i_finish; ++i){ - r = f$1(r, a[i], i); - } - return r; -} - function findIndexOpt(array, finder) { var index = array.findIndex(finder); if (index !== -1) { @@ -137,7 +128,6 @@ export { sort , indexOfOpt , lastIndexOfOpt , - reduceWithIndex , findIndexOpt , reverse , filterMap , diff --git a/src/Core__Array.res b/src/Core__Array.res index aecb7d5d..33b8188f 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -128,17 +128,7 @@ let sort = (arr, cmp) => { @send external mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b> = "map" @send external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce" - -let reduceWithIndexU = (a, x, f) => { - let r = ref(x) - for i in 0 to length(a) - 1 { - r.contents = f(. r.contents, getUnsafe(a, i), i) - } - r.contents -} - -let reduceWithIndex = (a, x, f) => reduceWithIndexU(a, x, (. a, b, c) => f(a, b, c)) - +@send external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce" @send external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 505997ae..f01983cf 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -73,25 +73,28 @@ let lastIndexOfOpt: (array<'a>, 'a) => option external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce" /** - `reduceRight(xs, f, init)` + `reduceWithIndex(xs, f, init)` - Works like `Array.reduce`; except that function `f` is applied to each item of `xs` from the last back to the first. + Applies `f` to each element of `xs` from beginning to end. Function `f` has three parameters: the item from the array and an “accumulator”, which starts with a value of `init` and the index of each element. `reduceWithIndex` returns the final value of the accumulator. ```res example - Array.reduceRight(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "dcba" + Array.reduceWithIndex([1, 2, 3, 4], (acc, x, i) => acc + x + i, 0) == 16 ``` */ @send -external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight" +external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce" /** - Applies `f` to each element of `xs` from beginning to end. Function `f` has three parameters: the item from the array and an “accumulator”, which starts with a value of `init` and the index of each element. `reduceWithIndex` returns the final value of the accumulator. + `reduceRight(xs, f, init)` + + Works like `Array.reduce`; except that function `f` is applied to each item of `xs` from the last back to the first. ```res example - Array.reduceWithIndex([1, 2, 3, 4], 0, (acc, x, i) => acc + x + i) == 16 + Array.reduceRight(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "dcba" ``` */ -let reduceWithIndex: (array<'a>, 'b, ('b, 'a, int) => 'b) => 'b +@send +external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight" @send external some: (array<'a>, 'a => bool) => bool = "some" @send external someWithIndex: (array<'a>, ('a, int) => bool) => bool = "some" diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs index 10c4c863..c4a01f5a 100644 --- a/test/ArrayTests.mjs +++ b/test/ArrayTests.mjs @@ -82,6 +82,49 @@ Test.run([ "ArrayTests.res", 18, 13, + 30 + ], + "reduceWithIndex" + ], [ + 1, + 2, + 3 + ].reduce((function (acc, v, i) { + return { + hd: v + i | 0, + tl: acc + }; + }), /* [] */0), eq, { + hd: 5, + tl: { + hd: 3, + tl: { + hd: 1, + tl: /* [] */0 + } + } + }); + +Test.run([ + [ + "ArrayTests.res", + 24, + 13, + 38 + ], + "reduceWithIndex - empty" + ], [].reduce((function (acc, v, i) { + return { + hd: v + i | 0, + tl: acc + }; + }), /* [] */0), eq, /* [] */0); + +Test.run([ + [ + "ArrayTests.res", + 31, + 13, 26 ], "reduceRight" @@ -103,7 +146,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 23, + 36, 20, 41 ], @@ -113,7 +156,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 25, + 38, 20, 38 ], @@ -133,7 +176,7 @@ var arr = [ Test.run([ [ "ArrayTests.res", - 28, + 41, 13, 38 ], @@ -143,7 +186,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 39, + 52, 13, 24 ], @@ -169,7 +212,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 44, + 57, 20, 42 ], @@ -188,7 +231,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 46, + 59, 13, 32 ], @@ -203,7 +246,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 52, + 65, 20, 30 ], @@ -220,7 +263,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 54, + 67, 13, 34 ], @@ -238,7 +281,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 59, + 72, 20, 41 ], @@ -252,7 +295,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 60, + 73, 20, 38 ], @@ -262,7 +305,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 63, + 76, 13, 22 ], @@ -284,7 +327,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 68, + 81, 20, 40 ], @@ -303,7 +346,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 70, + 83, 13, 30 ], diff --git a/test/ArrayTests.res b/test/ArrayTests.res index d5610803..0042e658 100644 --- a/test/ArrayTests.res +++ b/test/ArrayTests.res @@ -14,6 +14,19 @@ Test.run( Test.run(__POS_OF__("reduce"), Array.reduce([1, 2, 3], List.add, list{}), eq, list{3, 2, 1}) Test.run(__POS_OF__("reduce - empty"), Array.reduce([], List.add, list{}), eq, list{}) +Test.run( + __POS_OF__("reduceWithIndex"), + Array.reduceWithIndex([1, 2, 3], (acc, v, i) => list{v + i, ...acc}, list{}), + eq, + list{5, 3, 1}, +) +Test.run( + __POS_OF__("reduceWithIndex - empty"), + Array.reduceWithIndex([], (acc, v, i) => list{v + i, ...acc}, list{}), + eq, + list{}, +) + Test.run( __POS_OF__("reduceRight"), Array.reduceRight([1, 2, 3], List.add, list{}), From 8eb7433cc2548bed0a575efb7f9c6deaaf148b46 Mon Sep 17 00:00:00 2001 From: glennsl Date: Sun, 26 Feb 2023 15:31:39 +0100 Subject: [PATCH 13/18] feat(array): add reduceRightWithIndex --- src/Core__Array.res | 2 ++ src/Core__Array.resi | 12 ++++++++ test/ArrayTests.mjs | 67 ++++++++++++++++++++++++++++++++++++-------- test/ArrayTests.res | 13 +++++++++ 4 files changed, 82 insertions(+), 12 deletions(-) diff --git a/src/Core__Array.res b/src/Core__Array.res index 33b8188f..b47b4b32 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -131,6 +131,8 @@ let sort = (arr, cmp) => { @send external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce" @send external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight" +@send +external reduceRightWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduceRight" @send external some: (array<'a>, 'a => bool) => bool = "some" @send external someWithIndex: (array<'a>, ('a, int) => bool) => bool = "some" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index f01983cf..f49e7e92 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -96,6 +96,18 @@ external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce" @send external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight" +/** + `reduceRightWithIndex(xs, f, init)` + + Like `reduceRight`, but with an additional index argument on the callback function. + + ```res example + Array.reduceRightWithIndex([1, 2, 3, 4], (acc, x, i) => acc + x + i, 0) == 16 + ``` +*/ +@send +external reduceRightWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduceRight" + @send external some: (array<'a>, 'a => bool) => bool = "some" @send external someWithIndex: (array<'a>, ('a, int) => bool) => bool = "some" @get_index external get: (array<'a>, int) => option<'a> = "" diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs index c4a01f5a..9f8de3dd 100644 --- a/test/ArrayTests.mjs +++ b/test/ArrayTests.mjs @@ -156,7 +156,50 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 38, + 39, + 13, + 35 + ], + "reduceEightWithIndex" + ], [ + 1, + 2, + 3 + ].reduceRight((function (acc, v, i) { + return { + hd: v + i | 0, + tl: acc + }; + }), /* [] */0), eq, { + hd: 1, + tl: { + hd: 3, + tl: { + hd: 5, + tl: /* [] */0 + } + } + }); + +Test.run([ + [ + "ArrayTests.res", + 45, + 13, + 38 + ], + "reduceWithIndex - empty" + ], [].reduceRight((function (acc, v, i) { + return { + hd: v + i | 0, + tl: acc + }; + }), /* [] */0), eq, /* [] */0); + +Test.run([ + [ + "ArrayTests.res", + 51, 20, 38 ], @@ -176,7 +219,7 @@ var arr = [ Test.run([ [ "ArrayTests.res", - 41, + 54, 13, 38 ], @@ -186,7 +229,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 52, + 65, 13, 24 ], @@ -212,7 +255,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 57, + 70, 20, 42 ], @@ -231,7 +274,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 59, + 72, 13, 32 ], @@ -246,7 +289,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 65, + 78, 20, 30 ], @@ -263,7 +306,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 67, + 80, 13, 34 ], @@ -281,7 +324,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 72, + 85, 20, 41 ], @@ -295,7 +338,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 73, + 86, 20, 38 ], @@ -305,7 +348,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 76, + 89, 13, 22 ], @@ -327,7 +370,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 81, + 94, 20, 40 ], @@ -346,7 +389,7 @@ Test.run([ Test.run([ [ "ArrayTests.res", - 83, + 96, 13, 30 ], diff --git a/test/ArrayTests.res b/test/ArrayTests.res index 0042e658..74351ed4 100644 --- a/test/ArrayTests.res +++ b/test/ArrayTests.res @@ -35,6 +35,19 @@ Test.run( ) Test.run(__POS_OF__("reduceRight - empty"), Array.reduceRight([], List.add, list{}), eq, list{}) +Test.run( + __POS_OF__("reduceEightWithIndex"), + Array.reduceRightWithIndex([1, 2, 3], (acc, v, i) => list{v + i, ...acc}, list{}), + eq, + list{1, 3, 5}, +) +Test.run( + __POS_OF__("reduceWithIndex - empty"), + Array.reduceRightWithIndex([], (acc, v, i) => list{v + i, ...acc}, list{}), + eq, + list{}, +) + Test.run(__POS_OF__("shuffle - length"), Array.shuffle([1, 2, 3])->Array.length, eq, 3) Test.run( From b2f539ec8fce29b678cede3cb89e641e3d69837d Mon Sep 17 00:00:00 2001 From: glennsl Date: Sun, 26 Feb 2023 15:52:27 +0100 Subject: [PATCH 14/18] docs(array): add docs for make, fromInitializer, findMap and keepSome --- src/Core__Array.resi | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/Core__Array.resi b/src/Core__Array.resi index f49e7e92..ef574c26 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -5,8 +5,29 @@ external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> = "Array.from" @val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from" @val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from" + +/** + `make(~length, init)` + + Creates an array of length `length` initialized with the value of `init`. + + ```res example + Array.make(~length=3, #apple) == [#apple, #apple, #apple] + ``` +*/ let make: (~length: int, 'a) => array<'a> + +/** + `fromInitializer(~length, f)` + + Creates an array of length `length` initialized with the value returned from `f ` for each index. + + ```res example + Array.make(~length=3, i => i + 3) == [3, 4, 5] + ``` +*/ let fromInitializer: (~length: int, int => 'a) => array<'a> + @val external isArray: 'a => bool = "Array.isArray" @get external length: array<'a> => int = "length" @send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin" @@ -120,10 +141,31 @@ external setUnsafe: (array<'a>, int, 'a) => unit = "%array_unsafe_set" let findIndexOpt: (array<'a>, 'a => bool) => option let reverse: array<'a> => array<'a> let filterMap: (array<'a>, 'a => option<'b>) => array<'b> + +/** + `fromInitializer(~length, f)` + + Creates an array of length `length` initialized with the value returned from `f ` for each index. + + ```res example + Array.make(~length=3, i => i + 3) == [3, 4, 5] + ``` +*/ let keepSome: array> => array<'a> let shuffle: array<'a> => array<'a> let shuffleInPlace: array<'a> => unit @send external flatMap: (array<'a>, 'a => array<'b>) => array<'b> = "flatMap" + +/** + `findMap(arr, f)` + + Calls `f` for each element and returns the first value from `f` that is `Some(_)`. + Otherwise returns `None` + + ```res example + Array.findMap([1, 2, 3], n => mod(n, 2) ? Some(n - 2) : None) == 0 + ``` +*/ let findMap: (array<'a>, 'a => option<'b>) => option<'b> /** From ffc18ee9ab8bdc284836824b04eb503da08c16fa Mon Sep 17 00:00:00 2001 From: glennsl Date: Sun, 26 Feb 2023 15:52:58 +0100 Subject: [PATCH 15/18] docs(changelog): update changelog for array additions --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b5fe554..258cc59a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ - Change `Float.fromString` signature. Now accepts only string. https://github.com/rescript-association/rescript-core/pull/54 - Change `Float.parseFloat` signature. Now accepts only string. https://github.com/rescript-association/rescript-core/pull/54 - Add `getExn`, `getUnsafe`, `getWithDefault`, `map`, `mapWithDefault` and `flatMap` to `Nullable`. https://github.com/rescript-association/rescript-core/pull/67 +- Add `make`, `fromInitializer`, `findMap`, `keepSome`, `reduceRight` and `reduceRightWithIndex`. https://github.com/rescript-association/rescript-core/pull/49 +- Remove `reduceReverse` in favor of `reduceRight`. https://github.com/rescript-association/rescript-core/pull/49 +- Fixed type signatures of `reduce` and `reduceWithIndex`. https://github.com/rescript-association/rescript-core/pull/49 ### Documentation From b80a80f10b4c925d6f91fe06c7493a876efc64a8 Mon Sep 17 00:00:00 2001 From: glennsl Date: Tue, 28 Feb 2023 20:02:08 +0100 Subject: [PATCH 16/18] docs(array): fix docstring fro keepSome --- src/Core__Array.resi | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Core__Array.resi b/src/Core__Array.resi index ef574c26..6efdda99 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -143,12 +143,13 @@ let reverse: array<'a> => array<'a> let filterMap: (array<'a>, 'a => option<'b>) => array<'b> /** - `fromInitializer(~length, f)` + `keepSome(arr)` - Creates an array of length `length` initialized with the value returned from `f ` for each index. + Returns a new array containing `value` for all elements that are `Some(value)` + and ignoring every value that is `None` ```res example - Array.make(~length=3, i => i + 3) == [3, 4, 5] + Array.keepSome([Some(1), None, Some(3)]) == [1, 3] ``` */ let keepSome: array> => array<'a> From 6d4f25641fbdedfacfaedc6ab5008b6618fdc644 Mon Sep 17 00:00:00 2001 From: glennsl Date: Tue, 28 Feb 2023 20:06:32 +0100 Subject: [PATCH 17/18] fix(array): reorder args of reduce functions for better te inference --- src/Core__Array.mjs | 20 ++++++++++++++++ src/Core__Array.res | 4 ++++ src/Core__Array.resi | 12 ++++------ test/ArrayTests.mjs | 56 ++++++++++++++++++++++---------------------- test/ArrayTests.res | 16 ++++++------- test/TempTests.mjs | 9 +++---- test/TempTests.res | 2 +- 7 files changed, 70 insertions(+), 49 deletions(-) diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index a9fea805..1560df8f 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -46,6 +46,22 @@ function sort(arr, cmp) { return result; } +function reduce(arr, init, f) { + return arr.reduce(f, init); +} + +function reduceWithIndex(arr, init, f) { + return arr.reduce(f, init); +} + +function reduceRight(arr, init, f) { + return arr.reduceRight(f, init); +} + +function reduceRightWithIndex(arr, init, f) { + return arr.reduceRight(f, init); +} + function findIndexOpt(array, finder) { var index = array.findIndex(finder); if (index !== -1) { @@ -128,6 +144,10 @@ export { sort , indexOfOpt , lastIndexOfOpt , + reduce , + reduceWithIndex , + reduceRight , + reduceRightWithIndex , findIndexOpt , reverse , filterMap , diff --git a/src/Core__Array.res b/src/Core__Array.res index b47b4b32..a1c2e853 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -128,11 +128,15 @@ let sort = (arr, cmp) => { @send external mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b> = "map" @send external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce" +let reduce = (arr, init, f) => reduce(arr, f, init) @send external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce" +let reduceWithIndex = (arr, init, f) => reduceWithIndex(arr, f, init) @send external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight" +let reduceRight = (arr, init, f) => reduceRight(arr, f, init) @send external reduceRightWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduceRight" +let reduceRightWithIndex = (arr, init, f) => reduceRightWithIndex(arr, f, init) @send external some: (array<'a>, 'a => bool) => bool = "some" @send external someWithIndex: (array<'a>, ('a, int) => bool) => bool = "some" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 6efdda99..59329856 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -90,8 +90,7 @@ let lastIndexOfOpt: (array<'a>, 'a) => option Array.reduce(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "abcd" ``` */ -@send -external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce" +let reduce: (array<'b>, 'a, ('a, 'b) => 'a) => 'a /** `reduceWithIndex(xs, f, init)` @@ -102,8 +101,7 @@ external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce" Array.reduceWithIndex([1, 2, 3, 4], (acc, x, i) => acc + x + i, 0) == 16 ``` */ -@send -external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce" +let reduceWithIndex: (array<'b>, 'a, ('a, 'b, int) => 'a) => 'a /** `reduceRight(xs, f, init)` @@ -114,8 +112,7 @@ external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce" Array.reduceRight(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "dcba" ``` */ -@send -external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight" +let reduceRight: (array<'b>, 'a, ('a, 'b) => 'a) => 'a /** `reduceRightWithIndex(xs, f, init)` @@ -126,8 +123,7 @@ external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight" Array.reduceRightWithIndex([1, 2, 3, 4], (acc, x, i) => acc + x + i, 0) == 16 ``` */ -@send -external reduceRightWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduceRight" +let reduceRightWithIndex: (array<'b>, 'a, ('a, 'b, int) => 'a) => 'a @send external some: (array<'a>, 'a => bool) => bool = "some" @send external someWithIndex: (array<'a>, ('a, int) => bool) => bool = "some" diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs index 9f8de3dd..7cc4a4c6 100644 --- a/test/ArrayTests.mjs +++ b/test/ArrayTests.mjs @@ -52,11 +52,11 @@ Test.run([ 28 ], "reduce" - ], [ - 1, - 2, - 3 - ].reduce(Core__List.add, /* [] */0), eq, { + ], Core__Array.reduce([ + 1, + 2, + 3 + ], /* [] */0, Core__List.add), eq, { hd: 3, tl: { hd: 2, @@ -75,7 +75,7 @@ Test.run([ 36 ], "reduce - empty" - ], [].reduce(Core__List.add, /* [] */0), eq, /* [] */0); + ], Core__Array.reduce([], /* [] */0, Core__List.add), eq, /* [] */0); Test.run([ [ @@ -85,16 +85,16 @@ Test.run([ 30 ], "reduceWithIndex" - ], [ - 1, - 2, - 3 - ].reduce((function (acc, v, i) { + ], Core__Array.reduceWithIndex([ + 1, + 2, + 3 + ], /* [] */0, (function (acc, v, i) { return { hd: v + i | 0, tl: acc }; - }), /* [] */0), eq, { + })), eq, { hd: 5, tl: { hd: 3, @@ -113,12 +113,12 @@ Test.run([ 38 ], "reduceWithIndex - empty" - ], [].reduce((function (acc, v, i) { + ], Core__Array.reduceWithIndex([], /* [] */0, (function (acc, v, i) { return { hd: v + i | 0, tl: acc }; - }), /* [] */0), eq, /* [] */0); + })), eq, /* [] */0); Test.run([ [ @@ -128,11 +128,11 @@ Test.run([ 26 ], "reduceRight" - ], [ - 1, - 2, - 3 - ].reduceRight(Core__List.add, /* [] */0), eq, { + ], Core__Array.reduceRight([ + 1, + 2, + 3 + ], /* [] */0, Core__List.add), eq, { hd: 1, tl: { hd: 2, @@ -151,7 +151,7 @@ Test.run([ 41 ], "reduceRight - empty" - ], [].reduceRight(Core__List.add, /* [] */0), eq, /* [] */0); + ], Core__Array.reduceRight([], /* [] */0, Core__List.add), eq, /* [] */0); Test.run([ [ @@ -161,16 +161,16 @@ Test.run([ 35 ], "reduceEightWithIndex" - ], [ - 1, - 2, - 3 - ].reduceRight((function (acc, v, i) { + ], Core__Array.reduceRightWithIndex([ + 1, + 2, + 3 + ], /* [] */0, (function (acc, v, i) { return { hd: v + i | 0, tl: acc }; - }), /* [] */0), eq, { + })), eq, { hd: 1, tl: { hd: 3, @@ -189,12 +189,12 @@ Test.run([ 38 ], "reduceWithIndex - empty" - ], [].reduceRight((function (acc, v, i) { + ], Core__Array.reduceRightWithIndex([], /* [] */0, (function (acc, v, i) { return { hd: v + i | 0, tl: acc }; - }), /* [] */0), eq, /* [] */0); + })), eq, /* [] */0); Test.run([ [ diff --git a/test/ArrayTests.res b/test/ArrayTests.res index 74351ed4..40c352f7 100644 --- a/test/ArrayTests.res +++ b/test/ArrayTests.res @@ -11,39 +11,39 @@ Test.run( [3, 4, 5, 6, 7, 8, 9], ) -Test.run(__POS_OF__("reduce"), Array.reduce([1, 2, 3], List.add, list{}), eq, list{3, 2, 1}) -Test.run(__POS_OF__("reduce - empty"), Array.reduce([], List.add, list{}), eq, list{}) +Test.run(__POS_OF__("reduce"), Array.reduce([1, 2, 3], list{}, List.add), eq, list{3, 2, 1}) +Test.run(__POS_OF__("reduce - empty"), Array.reduce([], list{}, List.add), eq, list{}) Test.run( __POS_OF__("reduceWithIndex"), - Array.reduceWithIndex([1, 2, 3], (acc, v, i) => list{v + i, ...acc}, list{}), + Array.reduceWithIndex([1, 2, 3], list{}, (acc, v, i) => list{v + i, ...acc}), eq, list{5, 3, 1}, ) Test.run( __POS_OF__("reduceWithIndex - empty"), - Array.reduceWithIndex([], (acc, v, i) => list{v + i, ...acc}, list{}), + Array.reduceWithIndex([], list{}, (acc, v, i) => list{v + i, ...acc}), eq, list{}, ) Test.run( __POS_OF__("reduceRight"), - Array.reduceRight([1, 2, 3], List.add, list{}), + Array.reduceRight([1, 2, 3], list{}, List.add), eq, list{1, 2, 3}, ) -Test.run(__POS_OF__("reduceRight - empty"), Array.reduceRight([], List.add, list{}), eq, list{}) +Test.run(__POS_OF__("reduceRight - empty"), Array.reduceRight([], list{}, List.add), eq, list{}) Test.run( __POS_OF__("reduceEightWithIndex"), - Array.reduceRightWithIndex([1, 2, 3], (acc, v, i) => list{v + i, ...acc}, list{}), + Array.reduceRightWithIndex([1, 2, 3], list{}, (acc, v, i) => list{v + i, ...acc}), eq, list{1, 3, 5}, ) Test.run( __POS_OF__("reduceWithIndex - empty"), - Array.reduceRightWithIndex([], (acc, v, i) => list{v + i, ...acc}, list{}), + Array.reduceRightWithIndex([], list{}, (acc, v, i) => list{v + i, ...acc}), eq, list{}, ) diff --git a/test/TempTests.mjs b/test/TempTests.mjs index cfd7146b..9807a51a 100644 --- a/test/TempTests.mjs +++ b/test/TempTests.mjs @@ -4,6 +4,7 @@ import * as Core__Int from "../src/Core__Int.mjs"; import * as Core__Dict from "../src/Core__Dict.mjs"; import * as Core__JSON from "../src/Core__JSON.mjs"; import * as Caml_option from "rescript/lib/es6/caml_option.js"; +import * as Core__Array from "../src/Core__Array.mjs"; import * as Core__Float from "../src/Core__Float.mjs"; import * as Core__BigInt from "../src/Core__BigInt.mjs"; import * as Core__Option from "../src/Core__Option.mjs"; @@ -21,11 +22,11 @@ var array = [ 4 ]; -console.info(array.map(function (x) { - return (x << 1); - }).reduce((function (a, b) { +console.info(Core__Array.reduce(array.map(function (x) { + return (x << 1); + }), 0, (function (a, b) { return a + b | 0; - }), 0)); + }))); console.info(typeof array); diff --git a/test/TempTests.res b/test/TempTests.res index 9b7d5a6b..82784cea 100644 --- a/test/TempTests.res +++ b/test/TempTests.res @@ -4,7 +4,7 @@ Console.info("") Console.info("Array") Console.info("---") let array = [1, 2, 3, 4] -Console.info(array->Array.map(x => x * 2)->Array.reduce((a, b) => a + b, 0)) +Console.info(array->Array.map(x => x * 2)->Array.reduce(0, (a, b) => a + b)) Console.info(typeof(array)) Console.info("") From 616279f25b4005f6756f086985e801b51d9b4f70 Mon Sep 17 00:00:00 2001 From: glennsl Date: Tue, 28 Feb 2023 20:08:51 +0100 Subject: [PATCH 18/18] refactor(array): consistent type variable naming in reduce functions --- src/Core__Array.resi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 59329856..0bbb3984 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -90,7 +90,7 @@ let lastIndexOfOpt: (array<'a>, 'a) => option Array.reduce(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "abcd" ``` */ -let reduce: (array<'b>, 'a, ('a, 'b) => 'a) => 'a +let reduce: (array<'a>, 'b, ('b, 'a) => 'b) => 'b /** `reduceWithIndex(xs, f, init)` @@ -101,7 +101,7 @@ let reduce: (array<'b>, 'a, ('a, 'b) => 'a) => 'a Array.reduceWithIndex([1, 2, 3, 4], (acc, x, i) => acc + x + i, 0) == 16 ``` */ -let reduceWithIndex: (array<'b>, 'a, ('a, 'b, int) => 'a) => 'a +let reduceWithIndex: (array<'a>, 'b, ('b, 'a, int) => 'b) => 'b /** `reduceRight(xs, f, init)` @@ -112,7 +112,7 @@ let reduceWithIndex: (array<'b>, 'a, ('a, 'b, int) => 'a) => 'a Array.reduceRight(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "dcba" ``` */ -let reduceRight: (array<'b>, 'a, ('a, 'b) => 'a) => 'a +let reduceRight: (array<'a>, 'b, ('b, 'a) => 'b) => 'b /** `reduceRightWithIndex(xs, f, init)` @@ -123,7 +123,7 @@ let reduceRight: (array<'b>, 'a, ('a, 'b) => 'a) => 'a Array.reduceRightWithIndex([1, 2, 3, 4], (acc, x, i) => acc + x + i, 0) == 16 ``` */ -let reduceRightWithIndex: (array<'b>, 'a, ('a, 'b, int) => 'a) => 'a +let reduceRightWithIndex: (array<'a>, 'b, ('b, 'a, int) => 'b) => 'b @send external some: (array<'a>, 'a => bool) => bool = "some" @send external someWithIndex: (array<'a>, ('a, int) => bool) => bool = "some"