package tablecloth-native
Library
Module
Module type
Parameter
Class
Class type
A mutable vector of elements which must have the same type with O(1) get
and set
operations.
You can create an array
in OCaml with the [|1; 2; 3|]
syntax.
Create an array with only one element.
Array.singleton 1234 = [|1234|]
Array.singleton "hi" = [|"hi"|]
Return the length of an array.
Array.length [|1; 2, 3|] = 3
Array.length [||] = 0
Determine if an array is empty.
Array.isEmpty Array.empty = true
Array.isEmpty [||] = true
Array.isEmpty [|1; 2; 3|] = false
Initialize an array. Array.initialize ~length:n ~f
creates an array of length n
with the element at index i
initialized to the result of (f i)
.
Array.initialize ~length:4 ~f:identity = [|0; 1; 2; 3|]
Array.initialize ~length:4 ~f:(fun n -> n * n) = [|0; 1; 4; 9|]
Creates an array of length length
with the value x
populated at each index.
repeat ~length:5 'a' = [|'a'; 'a'; 'a'; 'a'; 'a'|]
repeat ~length:0 7 = [||]
repeat ~length:(-1) "Why?" = [||]
Creates an array containing all of the integers from from
if it is provided or 0
if not, up to but not including to
Array.range 5 = [|0; 1; 2; 3; 4|]
Array.range ~from: 2 5 = [|2; 3; 4|]
Array.range ~from:(-2) 3 = [|-2; -1; 0; 1; 2|]
Create an array from a List
.
Array.fromList [1;2;3] = [|1;2;3|]
Create a List
of elements from an array.
Array.toList [|1;2;3|] = [1;2;3]
Array.toList (Array.fromList [3; 5; 8]) = [3; 5; 8]
Array.get a n
returns, as an Option
, the element at index number n
of array a
.
The first element has index number 0.
The last element has index number Array.length a - 1
.
Returns None
if n
is outside the range 0
to (Array.length a - 1)
.
You can also write a.(n)
instead of Array.get n a
Array.get [|"cat"; "dog"; "eel"|] 2 = Some "eel"
Array.get [|0; 1; 2|] 5 = None
Array.get [||] 0 = None
Array.set a i x
modifies array a
in place, replacing the element at index number i
with x
.
You can also write a.(n) <- x
instead of Array.set a i x
.
Raises Invalid_argument "index out of bounds"
if i
is outside the range 0
to Array.length a - 1
.
Get the total of adding all of the integers in an array.
Array.sum [|1; 2; 3|] = 6
Get the total of adding all of the floating point numbers in an array.
Array.floatSum [|1.0; 2.0; 3.0|] = 6.0
Keep elements that f
returns true
for.
Array.filter ~f:Int.isEven [|1; 2; 3; 4; 5; 6|] = [|2; 4; 6|]
val swap : 'a t -> int -> int -> unit
Array.swap array i j
swaps the value at index i
with the value at index j
.
Array.swap [|1;2;3|] 1 2 = [|1;3;2|]
Create a new array which is the result of applying a function f
to every element.
Array.map ~f:sqrt [|1.0; 4.0; 9.0|] = [|1.0; 2.0; 3.0|]
Apply a function f
to every element with its index as the first argument.
Array.mapWithIndex ~f:( * ) [|5; 5; 5|] = [|0; 5; 10|]
Combine two arrays, using f
to combine each pair of elements. If one array is longer, the extra elements are dropped.
let totals (xs : int array) (ys : int array) : int array =
Array.map2 ~f:(+) xs ys in
totals [|1;2;3|] [|4;5;6|] = [|5;7;9|]
Array.map2
~f:Tuple2.create
[|"alice"; "bob"; "chuck"|]
[|2; 5; 7; 8|] =
[|("alice",2); ("bob",5); ("chuck",7)|]
Combine three arrays, using f
to combine each Tuple3
of elements. If one array is longer, the extra elements are dropped.
Array.map3
~f:Tuple3.create
[|"alice"; "bob"; "chuck"|]
[|2; 5; 7; 8;|]
[|true; false; true; false|] =
[|("alice", 2, true); ("bob", 5, false); ("chuck", 7, true)|]
Apply a function f
onto an array and flatten the resulting array of arrays.
Array.flatMap ~f xs = Array.map ~f xs |> Array.flatten
Array.flatMap ~f:(fun n -> [|n; n|]) [|1; 2; 3|] = [|1; 1; 2; 2; 3; 3|]
Returns as an option the first element for which f evaluates to true. If f
doesn't return true
for any of the elements find
will return None
Array.find ~f:Int.isEven [|1; 3; 4; 8;|] = Some 4
Array.find ~f:Int.isOdd [|0; 2; 4; 8;|] = None
Array.find ~f:Int.isEven [||] = None
Similar to Array.find
but f
is also called with the current index, and the return value will be a tuple of the index the passing value was found at and the passing value.
Array.findIndex ~f:(fun index number -> index > 2 && Int.isEven number) [|1; 3; 4; 8;|] = Some (3, 8)
Determine if f
returns true for any
values in an array.
Array.any ~f:isEven [|2;3|] = true
Array.any ~f:isEven [|1;3|] = false
Array.any ~f:isEven [||] = false
Determine if f
returns true for all
values in an array.
Array.all ~f:Int.isEven [|2;4|] = true
Array.all ~f:Int.isEven [|2;3|] = false
Array.all ~f:Int.isEven [||] = true
Creates a new array which is the result of appending the second array onto the end of the first.
let fortyTwos = Array.repeat ~length:2 42 in
let eightyOnes = Array.repeat ~length:3 81 in
Array.append fourtyTwos eightyOnes = [|42; 42; 81; 81; 81|];
Concatenate an array of arrays into a single array:
Array.concatenate [|[|1; 2|]; [|3|]; [|4; 5|]|] = [|1; 2; 3; 4; 5|]
Places sep
between all elements of the given array.
Array.intersperse ~sep:"on" [|"turtles"; "turtles"; "turtles"|] = [|"turtles"; "on"; "turtles"; "on"; "turtles"|]
Array.intersperse ~sep:0 [||] = [||]
Get a sub-section of an array. from
is a zero-based index where we will start our slice. The to_
is a zero-based index that indicates the end of the slice.
The slice extracts up to but not including to_
.
Array.slice ~from:0 ~to_:3 [|0; 1; 2; 3; 4|] = [|0; 1; 2|]
Array.slice ~from:1 ~to_:4 [|0; 1; 2; 3; 4|] = [|1; 2; 3|]
Array.slice ~from:5 ~to_:3 [|0; 1; 2; 3; 4|] = [||]
Both the from
and to_
indexes can be negative, indicating an offset from the end of the array.
Array.slice ~from:1 ~to_:(-1) [|0; 1; 2; 3; 4|] = [|1; 2; 3|]
Array.slice ~from:(-2) ~to_:5 [|0; 1; 2; 3; 4|] = [|3; 4|]
Array.slice ~from:(-2) ~to_:(-1) [|0; 1; 2; 3; 4|] = [|3|]
Provides a sliding 'window' of sub-arrays over an array.
The first sub-array starts at index 0
of the array and takes the first size
elements.
The sub-array then advances the index step
(which defaults to 1) positions before taking the next size
elements.
The sub-arrays are guaranteed to always be of length size
and iteration stops once a sub-array would extend beyond the end of the array.
Array.sliding [|1;2;3;4;5|] ~size:1 = [|[|1|]; [|2|]; [|3|]; [|4|]; [|5|]|]
Array.sliding [|1;2;3;4;5|] ~size:2 = [|[|1;2|]; [|2;3|]; [|3;4|]; [|4;5|]|]
Array.sliding [|1;2;3;4;5|] ~size:3 = [|[|1;2;3|]; [|2;3;4|]; [|3;4;5|]|]
Array.sliding [|1;2;3;4;5|] ~size:2 ~step:2 = [|[|1;2|]; [|3;4|]|]
Array.sliding [|1;2;3;4;5|] ~size:1 ~step:3 = [|[|1|]; [|4|]|]
Reduces collection to a value which is the accumulated result of running each element in the array through f
, where each successive invocation is supplied the return value of the previous.
Read foldLeft
as 'fold from the left'.
Array.foldLeft ~f:( * ) ~initial:1 (Array.repeat ~length:4 7) = 2401
Array.foldLeft ~f:((fun element list -> element :: list)) ~initial:[] [|1; 2; 3|] = [3; 2; 1]
This method is like foldLeft
except that it iterates over the elements of the array from right to left.
Array.foldRight ~f:(+) ~initial:0 (Array.repeat ~length:3 5) = 15
Array.foldRight ~f:(fun element list -> element :: list) ~initial:[] [|1; 2; 3|] = [1; 2; 3]
Create a new reversed array leaving the original untouched
let numbers = [|1; 2; 3|] in
Array.reverse numbers = [|3; 2; 1|];
numbers = [|1; 2; 3|];
Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
let numbers = [|1; 2; 3|] in
Array.reverseInPlace numbers;
numbers = [|3; 2; 1|]
Iterates over the elements of invokes f
for each element.
Array.forEach [|1; 2; 3|] ~f:(fun int -> print (Int.toString int))