From 9ce62a7b93ffc9f0a4c76d3f707b1d314e287a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Fri, 1 Mar 2024 15:55:59 +0000 Subject: [PATCH 1/2] Add Array.flatMapWithIndex --- src/Core__Array.res | 1 + src/Core__Array.resi | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Core__Array.res b/src/Core__Array.res index 165237b2..a8e6103e 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -230,6 +230,7 @@ let filterMap = (a, f) => { let keepSome = filterMap(_, x => x) @send external flatMap: (array<'a>, 'a => array<'b>) => array<'b> = "flatMap" +@send external flatMapWithIndex: (array<'a>, ('a, int) => 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 7fb2aae5..5132dcab 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -968,6 +968,30 @@ Console.log( @send external flatMap: (array<'a>, 'a => array<'b>) => array<'b> = "flatMap" +/** +`flatMapWithIndex(array, mapper)` returns a new array concatenating the arrays returned from running `mapper` on all items in `array`. + +## Examples +```rescript +type language = ReScript | TypeScript | JavaScript + +let array = [ReScript, TypeScript, JavaScript] + +Console.log( + array->Array.flatMapWithIndex((item, index) => + switch item { + | ReScript => [index] + | TypeScript => [index, index + 1] + | JavaScript => [index, index + 1, index + 2] + } + ), +) +// [0, 1, 2, 2, 3, 4] +``` +*/ +@send +external flatMapWithIndex: (array<'a>, ('a, int) => array<'b>) => array<'b> = "flatMap" + /** `findMap(arr, fn)` From 7d0dcf144d8e90f95a3eabf68667cece8a8c6bda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Fri, 1 Mar 2024 17:55:35 +0000 Subject: [PATCH 2/2] Add changelog entry for Array.flatMapWithIndex --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ebee649..eb5088e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Next version - Fix: Expose Intl.Common. https://github.com/rescript-association/rescript-core/pull/197 +- Add `Array.flatMapWithIndex` https://github.com/rescript-association/rescript-core/pull/199 ## 1.1.0