Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#### :rocket: New Feature

- Add `Array.filterMapWithIndex` to Stdlib. https://github.com/rescript-lang/rescript/pull/7876

#### :bug: Bug fix

- Fix code generation for emojis in polyvars and labels. https://github.com/rescript-lang/rescript/pull/7853
Expand Down
17 changes: 17 additions & 0 deletions packages/@rescript/runtime/Stdlib_Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,23 @@ let filterMap = (a, f) => {

let keepSome = filterMap(_, x => x)

let filterMapWithIndex = (a, f) => {
let l = length(a)
let r = makeUninitializedUnsafe(l)
let j = ref(0)
for i in 0 to l - 1 {
let v = getUnsafe(a, i)
switch f(v, i) {
| None => ()
| Some(v) =>
setUnsafe(r, j.contents, v)
j.contents = j.contents + 1
}
}
truncateToLengthUnsafe(r, j.contents)
r
}

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

Expand Down
18 changes: 18 additions & 0 deletions packages/@rescript/runtime/Stdlib_Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,24 @@ Array.filterMap([], n => mod(n, 2) == 0 ? Some(n * n) : None) == []
*/
let filterMap: (array<'a>, 'a => option<'b>) => array<'b>

/**
`filterMapWithIndex(array, fn)`

Calls `fn` for each element and returns a new array containing results of the `fn` calls which are not `None`.

## Examples

```rescript
["Hello", "Hi", "Good bye"]->Array.filterMapWithIndex((item, index) =>
switch item {
| "Hello" => Some(index)
| _ => None
}
) == [0]
```
*/
let filterMapWithIndex: (array<'a>, ('a, int) => option<'b>) => array<'b>

/**
`keepSome(arr)`

Expand Down
18 changes: 18 additions & 0 deletions packages/@rescript/runtime/lib/es6/Stdlib_Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ function keepSome(__x) {
return filterMap(__x, x => x);
}

function filterMapWithIndex(a, f) {
let l = a.length;
let r = new Array(l);
let j = 0;
for (let i = 0; i < l; ++i) {
let v = a[i];
let v$1 = f(v, i);
if (v$1 !== undefined) {
r[j] = Primitive_option.valFromOption(v$1);
j = j + 1 | 0;
}

}
r.length = j;
return r;
}

function findMap(arr, f) {
let _i = 0;
while (true) {
Expand Down Expand Up @@ -197,6 +214,7 @@ export {
findIndexOpt,
findLastIndexOpt,
filterMap,
filterMapWithIndex,
keepSome,
toShuffled,
shuffle,
Expand Down
18 changes: 18 additions & 0 deletions packages/@rescript/runtime/lib/js/Stdlib_Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ function keepSome(__x) {
return filterMap(__x, x => x);
}

function filterMapWithIndex(a, f) {
let l = a.length;
let r = new Array(l);
let j = 0;
for (let i = 0; i < l; ++i) {
let v = a[i];
let v$1 = f(v, i);
if (v$1 !== undefined) {
r[j] = Primitive_option.valFromOption(v$1);
j = j + 1 | 0;
}

}
r.length = j;
return r;
}

function findMap(arr, f) {
let _i = 0;
while (true) {
Expand Down Expand Up @@ -196,6 +213,7 @@ exports.reduceRightWithIndex = reduceRightWithIndex;
exports.findIndexOpt = findIndexOpt;
exports.findLastIndexOpt = findLastIndexOpt;
exports.filterMap = filterMap;
exports.filterMapWithIndex = filterMapWithIndex;
exports.keepSome = keepSome;
exports.toShuffled = toShuffled;
exports.shuffle = shuffle;
Expand Down
6 changes: 6 additions & 0 deletions tests/analysis_tests/tests/src/expected/Completion.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ Path Array.
"tags": [],
"detail": "(array<'a>, ('a, 'a) => Ordering.t) => unit",
"documentation": {"kind": "markdown", "value": "\n`sort(array, comparator)` sorts `array` in-place using the `comparator` function.\n\nBeware this will *mutate* the array.\n\nSee [`Array.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) on MDN.\n\n## Examples\n\n```rescript\nlet array = [3, 2, 1]\narray->Array.sort((a, b) => float(a - b))\narray == [1, 2, 3]\n```\n"}
}, {
"label": "filterMapWithIndex",
"kind": 12,
"tags": [],
"detail": "(array<'a>, ('a, int) => option<'b>) => array<'b>",
"documentation": {"kind": "markdown", "value": "\n`filterMapWithIndex(array, fn)`\n\nCalls `fn` for each element and returns a new array containing results of the `fn` calls which are not `None`.\n\n## Examples\n\n```rescript\n[\"Hello\", \"Hi\", \"Good bye\"]->Array.filterMapWithIndex((item, index) =>\n switch item {\n | \"Hello\" => Some(index)\n | _ => None\n }\n) == [0]\n```\n"}
}, {
"label": "length",
"kind": 12,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,18 @@ Path filt
"range": {"start": {"line": 86, "character": 34}, "end": {"line": 86, "character": 35}},
"newText": ""
}]
}, {
"label": "->Array.filterMapWithIndex",
"kind": 12,
"tags": [],
"detail": "(array<'a>, ('a, int) => option<'b>) => array<'b>",
"documentation": {"kind": "markdown", "value": "\n`filterMapWithIndex(array, fn)`\n\nCalls `fn` for each element and returns a new array containing results of the `fn` calls which are not `None`.\n\n## Examples\n\n```rescript\n[\"Hello\", \"Hi\", \"Good bye\"]->Array.filterMapWithIndex((item, index) =>\n switch item {\n | \"Hello\" => Some(index)\n | _ => None\n }\n) == [0]\n```\n"},
"sortText": "filterMapWithIndex",
"insertText": "->Array.filterMapWithIndex",
"additionalTextEdits": [{
"range": {"start": {"line": 86, "character": 34}, "end": {"line": 86, "character": 35}},
"newText": ""
}]
}, {
"label": "->Array.filter",
"kind": 12,
Expand Down
Loading