I have two different results from running the following code, one is in JS and another the same code embedded using raw embed.
let arrayMove : (array(int), int, int) => array(int) = [%raw
(arr, previousIndex, newIndex) =>
"{
const array = arr.slice(0);
if (newIndex >= array.length) {
let k = newIndex - array.length;
while (k-- + 1) {
array.push(undefined);
}
}
array.splice(newIndex, 0, array.splice(previousIndex, 1)[0]);
return array;
}"
];
If I run the JS snippet in the console, I get the correct output,
> arrayMove([1, 2, 3, 4], 0, 3)
[2, 3, 4, 1]
Now in Reason though I get a different output.
arrayMove([|1, 2, 3, 4|], 0, 3)
that gives me [| 1, 2, 3, 4 |]
.
What’s the reason behind this strange behavior? Initially I thought it had to do with immutability but array in Reason is mutable and it maps straightforwardly to JavaScript arrays.