OCaml Array Slice?

I am learning OCaml, and one thing that I could not learn how to do is to take a fragment of the array. For example, if I want to extract subarrays of 3 elements starting with index 2, I have to do: [|array.(2); array.(3); array.(4)|]. This is tiring. Is there a function that can quickly and easily slice an array? If not, how do I proceed with my own function for this behavior?

Really appreciate the help, thanks!

+4
source share
2 answers

Array.sub is easier to use than blit:

let v=[|0;1;2;3;4|];;
let v'=Array.sub v 2 3;;
# val v' : int array = [|2; 3; 4|]

Array.sub start len ​​returns a new array of length len containing the element number starting at the beginning + len - 1 of array a.

+9

Array.blit v1 o1 v2 o2 len len- v1, o1, v2, o2.

Array.init int → (int → 'a) → ' :

let b = Array.init 3 (fun x → array. (x + 2));;

+3

Source: https://habr.com/ru/post/1664640/


All Articles