-
Notifications
You must be signed in to change notification settings - Fork 15
/
ArrayExt.res
37 lines (35 loc) · 909 Bytes
/
ArrayExt.res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
let findIndexOf = (arr: array<'a>, x: 'a) =>
switch arr->Js.Array2.findIndex(x' => x' === x) {
| -1 => failwith(`Unable to find \`$(x)\` in array \`$(arr)\``)
| _ as i => i
}
let insert = (arr: array<'a>, ~value: 'a, ~place: [#Before('a) | #Last]) => {
let arr = arr->Js.Array2.copy
arr
->Js.Array2.spliceInPlace(
~pos=switch place {
| #Before(x) => arr->findIndexOf(x)
| #Last => arr->Js.Array.length
},
~remove=0,
~add=[value],
)
->ignore
arr
}
let reinsert = (arr: array<'a>, ~value: 'a, ~place: [#Before('a) | #Last]) => {
let arr = arr->Js.Array.copy
let from = arr->findIndexOf(value)
arr->Js.Array2.spliceInPlace(~pos=from, ~remove=1, ~add=[])->ignore
arr
->Js.Array2.spliceInPlace(
~pos=switch place {
| #Before(x) => arr->findIndexOf(x)
| #Last => arr->Js.Array.length
},
~remove=0,
~add=[value],
)
->ignore
arr
}