Skip to content

Add Array.flatMapWithIndex #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/Core__Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
24 changes: 24 additions & 0 deletions src/Core__Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -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)`

Expand Down