diff --git a/CHANGELOG.md b/CHANGELOG.md index cc992beb..b28f5fd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,9 @@ - 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 `getExn`, `getUnsafe`, `getWithDefault`, `map`, `mapWithDefault` and `flatMap` to `Null`. https://github.com/rescript-association/rescript-core/pull/73 +- 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 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/src/Core__Array.mjs b/src/Core__Array.mjs index f675dff5..1560df8f 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -3,7 +3,26 @@ 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(length, x) { + if (length <= 0) { + return []; + } + var arr = new Array(length); + arr.fill(x); + return arr; +} + +function fromInitializer(length, f) { + if (length <= 0) { + return []; + } + var arr = new Array(length); + for(var i = 0; i < length; ++i){ + arr[i] = Curry._1(f, i); + } + return arr; +} function indexOfOpt(arr, item) { var index = arr.indexOf(item); @@ -27,31 +46,20 @@ 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 reduce(arr, init, f) { + return arr.reduce(f, init); } -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 reduceWithIndex(arr, init, f) { + return arr.reduce(f, init); } -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 reduceRight(arr, init, f) { + return arr.reduceRight(f, init); +} + +function reduceRightWithIndex(arr, init, f) { + return arr.reduceRight(f, init); } function findIndexOpt(array, finder) { @@ -108,22 +116,44 @@ function filterMap(a, f) { return r; } -function flatMap(a, f) { - return Caml_splice_call.spliceObjApply([], "concat", [a.map(f)]); +function keepSome(__x) { + return filterMap(__x, (function (x) { + return x; + })); +} + +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 { + make , + fromInitializer , sort , indexOfOpt , lastIndexOfOpt , reduce , - reduceReverse , reduceWithIndex , + reduceRight , + reduceRightWithIndex , findIndexOpt , reverse , filterMap , + keepSome , shuffle , shuffleInPlace , - flatMap , + findMap , } /* No side effect */ diff --git a/src/Core__Array.res b/src/Core__Array.res index 658f45c8..a1c2e853 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,32 @@ 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 = (~length, x) => + if length <= 0 { + [] + } else { + let arr = makeUninitializedUnsafe(length) + arr->fillAllInPlace(x) + arr + } + +let fromInitializer = (~length, f) => + if length <= 0 { + [] + } else { + let arr = makeUninitializedUnsafe(length) + for i in 0 to length - 1 { + arr->setUnsafe(i, f(i)) + } + arr + } + @val external isArray: 'a => bool = "Array.isArray" @get external length: array<'a> => int = "length" @@ -23,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" @@ -105,35 +127,16 @@ 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)) - -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)) - -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 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" @@ -151,8 +154,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)) @@ -200,7 +201,22 @@ let filterMapU = (a, f) => { let filterMap = (a, f) => filterMapU(a, (. a) => f(a)) -// TODO: Change this implementation? -let flatMap = (a, f) => []->concatMany(map(a, f)) +let keepSome = filterMap(_, x => x) + +@send external flatMap: (array<'a>, 'a => array<'b>) => array<'b> = "flatMap" + +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..0bbb3984 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -5,6 +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" @@ -57,37 +80,50 @@ 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" + ``` +*/ +let reduce: (array<'a>, 'b, ('b, 'a) => 'b) => 'b + +/** + `reduceWithIndex(xs, f, init)` + + 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. - Array.reduce(["a", "b", "c", "d"], "", (a, b) => a ++ b) == "abcd" + ```res example + Array.reduceWithIndex([1, 2, 3, 4], (acc, x, i) => acc + x + i, 0) == 16 ``` */ -let reduce: (array<'b>, 'a, ('a, 'b) => 'a) => 'a +let reduceWithIndex: (array<'a>, 'b, ('b, 'a, int) => 'b) => 'b /** - `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 +let reduceRight: (array<'a>, 'b, ('b, 'a) => 'b) => 'b /** - 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. + `reduceRightWithIndex(xs, f, init)` + + Like `reduceRight`, but with an additional index argument on the callback function. ```res example - Array.reduceWithIndex([1, 2, 3, 4], 0, (acc, x, i) => acc + x + i) == 16 + Array.reduceRightWithIndex([1, 2, 3, 4], (acc, x, i) => acc + x + i, 0) == 16 ``` */ -let reduceWithIndex: (array<'a>, 'b, ('b, 'a, int) => 'b) => 'b +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" @@ -101,11 +137,35 @@ 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> + +/** + `keepSome(arr)` + + Returns a new array containing `value` for all elements that are `Some(value)` + and ignoring every value that is `None` + + ```res example + Array.keepSome([Some(1), None, Some(3)]) == [1, 3] + ``` +*/ +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" + +/** + `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> + +/** `at(array, index)` Get an element by its index. Negative indices count backwards from the last item. diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs new file mode 100644 index 00000000..7cc4a4c6 --- /dev/null +++ b/test/ArrayTests.mjs @@ -0,0 +1,407 @@ +// 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, + 30 + ], + "reduceWithIndex" + ], Core__Array.reduceWithIndex([ + 1, + 2, + 3 + ], /* [] */0, (function (acc, v, i) { + return { + hd: v + i | 0, + tl: acc + }; + })), eq, { + hd: 5, + tl: { + hd: 3, + tl: { + hd: 1, + tl: /* [] */0 + } + } + }); + +Test.run([ + [ + "ArrayTests.res", + 24, + 13, + 38 + ], + "reduceWithIndex - empty" + ], Core__Array.reduceWithIndex([], /* [] */0, (function (acc, v, i) { + return { + hd: v + i | 0, + tl: acc + }; + })), eq, /* [] */0); + +Test.run([ + [ + "ArrayTests.res", + 31, + 13, + 26 + ], + "reduceRight" + ], Core__Array.reduceRight([ + 1, + 2, + 3 + ], /* [] */0, Core__List.add), eq, { + hd: 1, + tl: { + hd: 2, + tl: { + hd: 3, + tl: /* [] */0 + } + } + }); + +Test.run([ + [ + "ArrayTests.res", + 36, + 20, + 41 + ], + "reduceRight - empty" + ], Core__Array.reduceRight([], /* [] */0, Core__List.add), eq, /* [] */0); + +Test.run([ + [ + "ArrayTests.res", + 39, + 13, + 35 + ], + "reduceEightWithIndex" + ], Core__Array.reduceRightWithIndex([ + 1, + 2, + 3 + ], /* [] */0, (function (acc, v, i) { + return { + hd: v + i | 0, + tl: acc + }; + })), eq, { + hd: 1, + tl: { + hd: 3, + tl: { + hd: 5, + tl: /* [] */0 + } + } + }); + +Test.run([ + [ + "ArrayTests.res", + 45, + 13, + 38 + ], + "reduceWithIndex - empty" + ], Core__Array.reduceRightWithIndex([], /* [] */0, (function (acc, v, i) { + return { + hd: v + i | 0, + tl: acc + }; + })), eq, /* [] */0); + +Test.run([ + [ + "ArrayTests.res", + 51, + 20, + 38 + ], + "shuffle - length" + ], Core__Array.shuffle([ + 1, + 2, + 3 + ]).length, eq, 3); + +var arr = [ + 1, + 2, + 3 +]; + +Test.run([ + [ + "ArrayTests.res", + 54, + 13, + 38 + ], + "shuffleInPlace - length" + ], (Core__Array.shuffleInPlace(arr), arr.length), eq, 3); + +Test.run([ + [ + "ArrayTests.res", + 65, + 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", + 70, + 20, + 42 + ], + "filterMap - no match" + ], Core__Array.filterMap([ + 1, + 2, + 3, + 4, + 5, + 6 + ], (function (param) { + + })), eq, []); + +Test.run([ + [ + "ArrayTests.res", + 72, + 13, + 32 + ], + "filterMap - empty" + ], Core__Array.filterMap([], (function (n) { + if (n % 2 === 0) { + return Math.imul(n, n); + } + + })), eq, []); + +Test.run([ + [ + "ArrayTests.res", + 78, + 20, + 30 + ], + "keepSome" + ], Core__Array.keepSome([ + 1, + undefined, + 3 + ]), eq, [ + 1, + 3 + ]); + +Test.run([ + [ + "ArrayTests.res", + 80, + 13, + 34 + ], + "keepSome - all Some" + ], Core__Array.keepSome([ + 1, + 2, + 3 + ]), eq, [ + 1, + 2, + 3 + ]); + +Test.run([ + [ + "ArrayTests.res", + 85, + 20, + 41 + ], + "keepSome - all None" + ], Core__Array.keepSome([ + undefined, + undefined, + undefined + ]), eq, []); + +Test.run([ + [ + "ArrayTests.res", + 86, + 20, + 38 + ], + "keepSome - empty" + ], Core__Array.keepSome([]), eq, []); + +Test.run([ + [ + "ArrayTests.res", + 89, + 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", + 94, + 20, + 40 + ], + "findMap - no match" + ], Core__Array.findMap([ + 1, + 2, + 3, + 4, + 5, + 6 + ], (function (param) { + + })), eq, undefined); + +Test.run([ + [ + "ArrayTests.res", + 96, + 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..40c352f7 --- /dev/null +++ b/test/ArrayTests.res @@ -0,0 +1,100 @@ +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__("reduceWithIndex"), + 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([], list{}, (acc, v, i) => list{v + i, ...acc}), + eq, + list{}, +) + +Test.run( + __POS_OF__("reduceRight"), + Array.reduceRight([1, 2, 3], list{}, List.add), + eq, + list{1, 2, 3}, +) +Test.run(__POS_OF__("reduceRight - empty"), Array.reduceRight([], list{}, List.add), eq, list{}) + +Test.run( + __POS_OF__("reduceEightWithIndex"), + 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([], list{}, (acc, v, i) => list{v + i, ...acc}), + 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