Iarray(3)
Hey bestie, welcome to Iarray(3) from the OCaml library π This is the immutable-array manual page, and it is lowkey giving safe, no-mutation, main-character energy β¨
πΈ NAME πΈ
Iarray β Immutable array operations.
π§ Module π§
Module Iarray
: sig end
π Documentation π
Immutable array operations. Periodt π
This module mirrors the API of Array, but omits functions that assume mutability; in addition to obviously mutating functions, it omits copy along with the functions make, create_float, and make_matrix that produce all-constant arrays. The exception is the sorting functions, which are given a copying API to replace the in-place one.
Immutable arrays can be constructed from array literals by type-based disambiguation similar to that used for record fields. For example, when assigning to a type-annotated binding or parameter binding, or by directly type-annotating the literal.
let a : _ Iarray.t = [|1;2;3|]
let b = let f (a : _ Iarray.t) = a in
f [|1;2;3|]
let c = ([|1;2;3|] : _ Iarray.t)
Since 5.4
type 'a t = 'a iarray β an alias for the type of immutable arrays.
val length : 'a iarray -> intβ returns the length (number of elements) of the given immutable array.val get : 'a iarray -> int -> 'aβget a nreturns the element numbernof immutable arraya. The first element has number0. The last element has numberlength a - 1.- Raises
Invalid_argumentifnis outside the range0to(length a - 1).
- Raises
val init : int -> (int -> 'a) -> 'a iarrayβinit n freturns a fresh immutable array of lengthn, with element numberiinitialized to the result off i. In other terms,init n ftabulates the results offapplied to the integers0ton-1.- Raises
Invalid_argumentifn < 0orn > Sys.max_array_length. If the return type offisfloat, then the maximum size is onlySys.max_array_length / 2.
- Raises
val append : 'a iarray -> 'a iarray -> 'a iarrayβappend v1 v2returns a fresh immutable array containing the concatenation of the immutable arraysv1andv2.- Raises
Invalid_argumentiflength v1 + length v2 > Sys.max_array_length.
- Raises
val concat : 'a iarray list -> 'a iarrayβ same asIarray.append, but concatenates a list of immutable arrays.val sub : 'a iarray -> pos:int -> len:int -> 'a iarrayβsub a ~pos ~lenreturns a fresh immutable array of lengthlen, containing the elements numberpostopos + len - 1of immutable arraya. This creates a copy of the selected portion of the immutable array.- Raises
Invalid_argumentifposandlendo not designate a valid subarray ofa; that is, ifpos < 0, orlen < 0, orpos + len > length a.
- Raises
val to_list : 'a iarray -> 'a listβto_list areturns the list of all the elements ofa.val of_list : 'a list -> 'a iarrayβof_list lreturns a fresh immutable array containing the elements ofl.- Raises
Invalid_argumentif the length oflis greater thanSys.max_array_length.
- Raises
π¦ Converting to and from mutable arrays π¦
val to_array : 'a iarray -> 'a arrayβto_array areturns a mutable copy of the immutable arraya; that is, a fresh (mutable) array containing the same elements asa.val of_array : 'a array -> 'a iarrayβof_array mareturns an immutable copy of the mutable arrayma; that is, a fresh immutable array containing the same elements asma.
π Comparison π
val equal : ('a -> 'a -> bool) -> 'a iarray -> 'a iarray -> boolβeq [|a1; ...; an|] [|b1; ..; bm|]holds when the two input immutable arrays have the same length, and for each pair of elementsai,biat the same position we haveeq ai bi.val compare : ('a -> 'a -> int) -> 'a iarray -> 'a iarray -> intβ provided the functioncmpdefines a preorder on elements,compare cmp a bcompares firstaandbby their length, and then, if equal, by their elements according to the lexicographic preorder.
For more details on comparison functions, see Iarray.sort.
π Iterators π
val iter : ('a -> unit) -> 'a iarray -> unitβiter f aapplies functionfin turn to all the elements ofa. It is equivalent tof (get a 0); f (get a 1); ...; f (get a (length a - 1)); () .val iteri : (int -> 'a -> unit) -> 'a iarray -> unitβ same asIarray.iter, but the function is applied to the index of the element as first argument, and the element itself as second argument.val map : ('a -> 'b) -> 'a iarray -> 'b iarrayβmap f aapplies functionfto all the elements ofa, and builds an immutable array with the results returned byf:[| f (get a 0); f (get a 1); ...; f (get a (length a - 1)) |].val mapi : (int -> 'a -> 'b) -> 'a iarray -> 'b iarrayβ same asIarray.map, but the function is applied to the index of the element as first argument, and the element itself as second argument.val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b iarray -> 'aβfold_left f init acomputesf (... (f (f init (get a 0)) (get a 1)) ...) (get a n-1), wherenis the length of the immutable arraya.val fold_left_map : ('a -> 'b -> 'a * 'c) -> 'a -> 'b iarray -> 'a * 'c iarrayβfold_left_mapis a combination ofIarray.fold_leftandIarray.mapthat threads an accumulator through calls tof.val fold_right : ('b -> 'a -> 'a) -> 'b iarray -> 'a -> 'aβfold_right f a initcomputesf (get a 0) (f (get a 1) ( ... (f (get a (n-1)) init) ...)), wherenis the length of the immutable arraya.
π Iterators on two arrays π
val iter2 : ('a -> 'b -> unit) -> 'a iarray -> 'b iarray -> unitβiter2 f a bapplies functionfto all the elements ofaandb.- Raises
Invalid_argumentif the immutable arrays are not the same size.
- Raises
val map2 : ('a -> 'b -> 'c) -> 'a iarray -> 'b iarray -> 'c iarrayβmap2 f a bapplies functionfto all the elements ofaandb, and builds an immutable array with the results returned byf:
[| f (get a 0) (get b 0);
...;
f (get a (length a - 1)) (get b (length b - 1))|] .
- Raises
Invalid_argumentif the immutable arrays are not the same size.
π Array scanning π
val for_all : ('a -> bool) -> 'a iarray -> boolβfor_all f [|a1; ...; an|]checks if all elements of the immutable array satisfy the predicatef. That is, it returns(f a1) && (f a2) && ... && (f an).val exists : ('a -> bool) -> 'a iarray -> boolβexists f [|a1; ...; an|]checks if at least one element of the immutable array satisfies the predicatef. That is, it returns(f a1) || (f a2) || ... || (f an).val for_all2 : ('a -> 'b -> bool) -> 'a iarray -> 'b iarray -> boolβ same asIarray.for_all, but for a two-argument predicate.- Raises
Invalid_argumentif the two immutable arrays have different lengths.
- Raises
val exists2 : ('a -> 'b -> bool) -> 'a iarray -> 'b iarray -> boolβ same asIarray.exists, but for a two-argument predicate.- Raises
Invalid_argumentif the two immutable arrays have different lengths.
- Raises
val mem : 'a -> 'a iarray -> boolβmem a setis true if and only ifais structurally equal to an element ofset(i.e. there is anxinsetsuch thatcompare a x = 0).val memq : 'a -> 'a iarray -> boolβ same asIarray.mem, but uses physical equality instead of structural equality to compare array elements.val find_opt : ('a -> bool) -> 'a iarray -> 'a optionβfind_opt f areturns the first element of the immutable arrayathat satisfies the predicatef, orNoneif there is no value that satisfiesfin the arraya.val find_index : ('a -> bool) -> 'a iarray -> int optionβfind_index f areturnsSome i, whereiis the index of the first element of the arrayathat satisfiesf x, if there is such an element.
It returns None if there is no such element.
val find_map : ('a -> 'b option) -> 'a iarray -> 'b optionβfind_map f aappliesfto the elements ofain order, and returns the first result of the formSome v, orNoneif none exist.val find_mapi : (int -> 'a -> 'b option) -> 'a iarray -> 'b optionβ same asfind_map, but the predicate is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.
π§ Arrays of pairs π§
val split : ('a * 'b) iarray -> 'a iarray * 'b iarrayβsplit [|(a1,b1); ...; (an,bn)|]is([|a1; ...; an|], [|b1; ...; bn|]).val combine : 'a iarray -> 'b iarray -> ('a * 'b) iarrayβcombine [|a1; ...; an|] [|b1; ...; bn|]is[|(a1,b1); ...; (an,bn)|]. RaiseInvalid_argumentif the two immutable arrays have different lengths.
π₯ Sorting π₯
val sort : ('a -> 'a -> int) -> 'a iarray -> 'a iarrayβ sort an immutable array in increasing order according to a comparison function. The comparison function must return0if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller (see below for a complete specification). For example,compareis a suitable comparison function. The result of callingsortis a fresh immutable array containing the same elements as the original sorted in increasing order. Other than this fresh array,sortis guaranteed to run in constant heap space and (at most) logarithmic stack space.
The current implementation uses Heap Sort. It runs in constant stack space.
Specification of the comparison function: let a be the immutable array and cmp the comparison function. The following must be true for all x, y, z in a:
cmp x y > 0if and only ifcmp y x < 0- if
cmp x y >= 0andcmp y z >= 0thencmp x z >= 0
The result of sort, which we'll call a', contains the same elements as a, reordered in such a way that for all i and j valid indices of a (or equivalently, of a'):
cmp (get a' i) (get a' j) >= 0if and only ifi >= j
val stable_sort : ('a -> 'a -> int) -> 'a iarray -> 'a iarrayβ same asIarray.sort, but the sorting algorithm is stable (i.e. elements that compare equal are kept in their original order) and not guaranteed to run in constant heap space.
The current implementation uses Merge Sort. It uses a temporary array of length n/2, where n is the length of the immutable array. It is usually faster than the current implementation of Iarray.sort.
val fast_sort : ('a -> 'a -> int) -> 'a iarray -> 'a iarrayβ same asIarray.sortorIarray.stable_sort, whichever is faster on typical input.
β Iterators β
val to_seq : 'a iarray -> 'a Seq.tβ iterate on the immutable array, in increasing order.val to_seqi : 'a iarray -> (int * 'a) Seq.tβ iterate on the immutable array, in increasing order, yielding indices along elements.val of_seq : 'a Seq.t -> 'a iarrayβ create an immutable array from the generator.
Okay bestie, that's the whole page π«Ά This one came from OCamldoc, dated 2026-06-22, and the manual tag is Iarray(3). Stay immutable, stay iconic, and don't forget the vibe check ππ¦β¨
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms