Skip to content

Commit 33c82a9

Browse files
authored
Add Array.flatMapWithIndex (#199)
* Add Array.flatMapWithIndex * Add changelog entry for Array.flatMapWithIndex
1 parent 43daa8c commit 33c82a9

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Next version
44

55
- Fix: Expose Intl.Common. https://github.com/rescript-association/rescript-core/pull/197
6+
- Add `Array.flatMapWithIndex` https://github.com/rescript-association/rescript-core/pull/199
67

78
## 1.1.0
89

src/Core__Array.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ let filterMap = (a, f) => {
230230
let keepSome = filterMap(_, x => x)
231231

232232
@send external flatMap: (array<'a>, 'a => array<'b>) => array<'b> = "flatMap"
233+
@send external flatMapWithIndex: (array<'a>, ('a, int) => array<'b>) => array<'b> = "flatMap"
233234

234235
let findMap = (arr, f) => {
235236
let rec loop = i =>

src/Core__Array.resi

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,30 @@ Console.log(
968968
@send
969969
external flatMap: (array<'a>, 'a => array<'b>) => array<'b> = "flatMap"
970970

971+
/**
972+
`flatMapWithIndex(array, mapper)` returns a new array concatenating the arrays returned from running `mapper` on all items in `array`.
973+
974+
## Examples
975+
```rescript
976+
type language = ReScript | TypeScript | JavaScript
977+
978+
let array = [ReScript, TypeScript, JavaScript]
979+
980+
Console.log(
981+
array->Array.flatMapWithIndex((item, index) =>
982+
switch item {
983+
| ReScript => [index]
984+
| TypeScript => [index, index + 1]
985+
| JavaScript => [index, index + 1, index + 2]
986+
}
987+
),
988+
)
989+
// [0, 1, 2, 2, 3, 4]
990+
```
991+
*/
992+
@send
993+
external flatMapWithIndex: (array<'a>, ('a, int) => array<'b>) => array<'b> = "flatMap"
994+
971995
/**
972996
`findMap(arr, fn)`
973997

0 commit comments

Comments
 (0)