Stdlib.Iarray(3)
π¦ Stdlib.Iarray π¦
okay bestie, welcome to the Stdlib.Iarray page from the OCaml library manual β yes, the manual name is folded right in near the top, you're welcome β¨. This is all about immutable arrays, and they are lowkey iconic. π
β¨ NAME β¨
Stdlib.Iarray β no description. That's literally it. The page says βno description,β so I'm not inventing lore. π
π§ Module π§
Module Stdlib.Iarray
πΈ Documentation πΈ
Module Iarray
: (module Stdlib__Iarray)
type 'a t = 'a iarrayβ it's an alias for the type of immutable arrays, periodt.val length : 'a iarray -> intβ returns the length (number of elements) of the given immutable array. slay.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.
It raises Invalid_argument if n is outside the range 0 to (length a - 1).
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.
It raises Invalid_argument if n < 0 or n > Sys.max_array_length. If the return type of f is float, then the maximum size is only Sys.max_array_length / 2.
val append : 'a iarray -> 'a iarray -> 'a iarrayβappend v1 v2returns a fresh immutable array containing the concatenation of the immutable arraysv1andv2.
It raises Invalid_argument if length v1 + length v2 > Sys.max_array_length.
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.
It raises Invalid_argument if pos and len do not designate a valid subarray of a; that is, if pos < 0, or len < 0, or pos + len > length a.
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.
It raises Invalid_argument if the length of l is greater than Sys.max_array_length.
π 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.
It raises Invalid_argument if the immutable arrays are not the same size.
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))|]
It raises Invalid_argument if 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.
It raises Invalid_argument if the two immutable arrays have different lengths.
val exists2 : ('a -> 'b -> bool) -> 'a iarray -> 'b iarray -> boolβ same asIarray.exists, but for a two-argument predicate.
It raises Invalid_argument if the two immutable arrays have different lengths.
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 from0), 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.
mwah π this page was brought to you by OCamldoc, dated 2026-06-22, for Stdlib.Iarray(3) β bye bestie, go slay some immutable arrays! β¨π«Ά
π¦ 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