Skip to content

Add Array.removeInPlace #7321

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 8 commits into from
Mar 21, 2025
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 @@ -16,6 +16,7 @@

- Add `Dict.has` and double `Dict.forEachWithKey`/`Dict.mapValues` performance. https://github.com/rescript-lang/rescript/pull/7316
- Add popover attributes to JsxDOM.domProps. https://github.com/rescript-lang/rescript/pull/7317
- Add `Array.removeInPlace` helper based on `splice`. https://github.com/rescript-lang/rescript/pull/7321
- Add `inert` attribute to `JsxDOM.domProps`. https://github.com/rescript-lang/rescript/pull/7326

#### :boom: Breaking Change
Expand Down
3 changes: 3 additions & 0 deletions runtime/Stdlib_Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => u
external toSpliced: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => array<'a> =
"toSpliced"

@send
external removeInPlace: (array<'a>, int, @as(1) _) => unit = "splice"

@send external with: (array<'a>, int, 'a) => array<'a> = "with"

@send external unshift: (array<'a>, 'a) => unit = "unshift"
Expand Down
20 changes: 20 additions & 0 deletions runtime/Stdlib_Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,26 @@ external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => u
external toSpliced: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => array<'a> =
"toSpliced"

/**
`removeInPlace(array, index)` removes the item at the specified `index` from `array`.

Beware this will *mutate* the array.

## Examples

```rescript
let array = []
array->Array.removeInPlace(0)
assertEqual(array, []) // Removing from an empty array does nothing

let array2 = ["Hello", "Hi", "Good bye"]
array2->Array.removeInPlace(1)
assertEqual(array2, ["Hello", "Good bye"]) // Removes the item at index 1
```
*/
@send
external removeInPlace: (array<'a>, int, @as(1) _) => unit = "splice"

@send external with: (array<'a>, int, 'a) => array<'a> = "with"

/**
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 @@ -508,6 +508,12 @@ Path Array.
"tags": [],
"detail": "(array<'a>, int) => option<'a>",
"documentation": {"kind": "markdown", "value": "\n`get(array, index)` returns the element at `index` of `array`.\n\nReturns `None` if the index does not exist in the array. Equivalent to doing `array[index]` in JavaScript.\n\n## Examples\n\n```rescript\nlet array = [\"Hello\", \"Hi\", \"Good bye\"]\n\narray\n->Array.get(0)\n->assertEqual(Some(\"Hello\"))\n\narray\n->Array.get(3)\n->assertEqual(None)\n```\n"}
}, {
"label": "removeInPlace",
"kind": 12,
"tags": [],
"detail": "(array<'a>, int) => unit",
"documentation": {"kind": "markdown", "value": "\n`removeInPlace(array, index)` removes the item at the specified `index` from `array`.\n\nBeware this will *mutate* the array.\n\n## Examples\n\n```rescript\nlet array = []\narray->Array.removeInPlace(0)\nassertEqual(array, []) // Removing from an empty array does nothing\n\nlet array2 = [\"Hello\", \"Hi\", \"Good bye\"]\narray2->Array.removeInPlace(1)\nassertEqual(array2, [\"Hello\", \"Good bye\"]) // Removes the item at index 1\n```\n "}
}, {
"label": "pushMany",
"kind": 12,
Expand Down
4 changes: 2 additions & 2 deletions tests/analysis_tests/tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ reset='\033[0m'

diff=$(git ls-files --modified src/expected)
if [[ $diff = "" ]]; then
printf "${successGreen}✅ No unstaged tests difference.${reset}\n"
printf "${successGreen}✅ No analysis_tests snapshot changes detected.${reset}\n"
else
printf "${warningYellow}⚠️ There are unstaged differences in tests/! Did you break a test?\n${diff}\n${reset}"
printf "${warningYellow}⚠️ The analysis_tests snapshot doesn't match. Double check that the output is correct, run 'make analysis_tests' and stage the diff.\n${diff}\n${reset}"
git --no-pager diff src/expected
exit 1
fi
38 changes: 38 additions & 0 deletions tests/tests/src/core/Core_ArrayTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,44 @@ Test.run([
"last - empty"
], Stdlib_Array.last([]), eq, undefined);

let array = [];

array.splice(1, 0, "foo");

Test.run([
[
"Core_ArrayTests.res",
116,
22,
49
],
"splice - Insert no delete"
], array, eq, ["foo"]);

let array$1 = [
"bar",
"baz"
];

Test.run([
[
"Core_ArrayTests.res",
122,
15,
43
],
"splice - Insert and delete"
], [
(array$1.splice(1, 1, "foo"), undefined),
array$1
], eq, [
undefined,
[
"bar",
"foo"
]
]);

export {
eq,
}
Expand Down
21 changes: 21 additions & 0 deletions tests/tests/src/core/Core_ArrayTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,24 @@ Test.run(

Test.run(__POS_OF__("last - with items"), [1, 2, 3]->Array.last, eq, Some(3))
Test.run(__POS_OF__("last - empty"), []->Array.last, eq, None)

{
let array = []
array->Array.splice(~start=1, ~remove=0, ~insert=["foo"])
Test.run(__POS_OF__("splice - Insert no delete"), array, eq, ["foo"])
}

{
let array = ["bar", "baz"]
Test.run(
__POS_OF__("splice - Insert and delete"),
(array->Array.splice(~start=1, ~remove=1, ~insert=["foo"]), array),
eq,
(
// Even though original .splice returns an array with the removed items,
// the binding returns unit so there's no confusion about it mutating the original array.
(),
["bar", "foo"],
),
)
}