Skip to content

Commit 1d2fa81

Browse files
committed
Add Array.removeInPlace
1 parent db89ef2 commit 1d2fa81

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

runtime/Stdlib_Array.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => u
108108
external toSpliced: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => array<'a> =
109109
"toSpliced"
110110

111+
@send
112+
external removeInPlace: (array<'a>, int, @as(1) _) => unit = "splice"
113+
111114
@send external with: (array<'a>, int, 'a) => array<'a> = "with"
112115

113116
@send external unshift: (array<'a>, 'a) => unit = "unshift"

runtime/Stdlib_Array.resi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => u
302302
external toSpliced: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => array<'a> =
303303
"toSpliced"
304304

305+
@send
306+
external removeInPlace: (array<'a>, int, @as(1) _) => unit = "splice"
307+
305308
@send external with: (array<'a>, int, 'a) => array<'a> = "with"
306309

307310
/**

tests/tests/src/core/Core_ArrayTests.mjs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,79 @@ Test.run([
457457
"last - empty"
458458
], Stdlib_Array.last([]), eq, undefined);
459459

460+
let array = [];
461+
462+
array.splice(1, 0, "foo");
463+
464+
Test.run([
465+
[
466+
"Core_ArrayTests.res",
467+
116,
468+
22,
469+
49
470+
],
471+
"splice - Insert no delete"
472+
], array, eq, ["foo"]);
473+
474+
let array$1 = [
475+
"bar",
476+
"baz"
477+
];
478+
479+
Test.run([
480+
[
481+
"Core_ArrayTests.res",
482+
122,
483+
15,
484+
43
485+
],
486+
"splice - Insert and delete"
487+
], [
488+
(array$1.splice(1, 1, "foo"), undefined),
489+
array$1
490+
], eq, [
491+
undefined,
492+
[
493+
"bar",
494+
"foo"
495+
]
496+
]);
497+
498+
let array$2 = [];
499+
500+
array$2.splice(0, 1);
501+
502+
Test.run([
503+
[
504+
"Core_ArrayTests.res",
505+
132,
506+
22,
507+
45
508+
],
509+
"removeInPlace - empty"
510+
], array$2, eq, []);
511+
512+
let array$3 = [
513+
"Hello",
514+
"Hi",
515+
"Good bye"
516+
];
517+
518+
array$3.splice(1, 1);
519+
520+
Test.run([
521+
[
522+
"Core_ArrayTests.res",
523+
138,
524+
22,
525+
51
526+
],
527+
"removeInPlace - from middle"
528+
], array$3, eq, [
529+
"Hello",
530+
"Good bye"
531+
]);
532+
460533
export {
461534
eq,
462535
}

tests/tests/src/core/Core_ArrayTests.res

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,36 @@ Test.run(
109109

110110
Test.run(__POS_OF__("last - with items"), [1, 2, 3]->Array.last, eq, Some(3))
111111
Test.run(__POS_OF__("last - empty"), []->Array.last, eq, None)
112+
113+
{
114+
let array = []
115+
array->Array.splice(~start=1, ~remove=0, ~insert=["foo"])
116+
Test.run(__POS_OF__("splice - Insert no delete"), array, eq, ["foo"])
117+
}
118+
119+
{
120+
let array = ["bar", "baz"]
121+
Test.run(
122+
__POS_OF__("splice - Insert and delete"),
123+
(array->Array.splice(~start=1, ~remove=1, ~insert=["foo"]), array),
124+
eq,
125+
(
126+
// Even though original .splice returns an array with the removed items,
127+
// the binding returns unit so there's no confusion about it mutating the original array.
128+
(),
129+
["bar", "foo"],
130+
),
131+
)
132+
}
133+
134+
{
135+
let array = []
136+
array->Array.removeInPlace(0)
137+
Test.run(__POS_OF__("removeInPlace - empty"), array, eq, [])
138+
}
139+
140+
{
141+
let array = ["Hello", "Hi", "Good bye"]
142+
array->Array.removeInPlace(1)
143+
Test.run(__POS_OF__("removeInPlace - from middle"), array, eq, ["Hello", "Good bye"])
144+
}

0 commit comments

Comments
 (0)