Skip to content

Commit 78badc1

Browse files
committed
Add Array.removeInPlace
1 parent db89ef2 commit 78badc1

File tree

4 files changed

+117
-2
lines changed

4 files changed

+117
-2
lines changed

runtime/Stdlib_Array.res

+4-1
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,14 @@ external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<
103103
@send external shift: array<'a> => option<'a> = "shift"
104104

105105
@variadic @send
106-
external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit = "splice"
106+
external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => array<'a> = "splice"
107107
@variadic @send
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

+4-1
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,15 @@ array->assertEqual([1, 2, 3])
296296
external sort: (array<'a>, ('a, 'a) => Stdlib_Ordering.t) => unit = "sort"
297297

298298
@variadic @send
299-
external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit = "splice"
299+
external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => array<'a> = "splice"
300300

301301
@variadic @send
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

+77
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,83 @@ Test.run([
457457
"last - empty"
458458
], Stdlib_Array.last([]), eq, undefined);
459459

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

tests/tests/src/core/Core_ArrayTests.res

+32
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,35 @@ 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+
Test.run(
116+
__POS_OF__("splice - Insert no delete"),
117+
(array->Array.splice(~start=1, ~remove=0, ~insert=["foo"]), array),
118+
eq,
119+
([], ["foo"]),
120+
)
121+
}
122+
123+
{
124+
let array = ["bar", "baz"]
125+
Test.run(
126+
__POS_OF__("splice - Insert and delete"),
127+
(array->Array.splice(~start=1, ~remove=1, ~insert=["foo"]), array),
128+
eq,
129+
(["baz"], ["bar", "foo"]),
130+
)
131+
}
132+
133+
{
134+
let array = []
135+
array->Array.removeInPlace(0)
136+
Test.run(__POS_OF__("removeInPlace - empty"), array, eq, [])
137+
}
138+
139+
{
140+
let array = ["Hello", "Hi", "Good bye"]
141+
array->Array.removeInPlace(1)
142+
Test.run(__POS_OF__("removeInPlace - from middle"), array, eq, ["Hello", "Good bye"])
143+
}

0 commit comments

Comments
 (0)