Module Base.ArraySource

Sourcemodule type Public = sig ... end
Sourcetype ('a : any mod separable) t = 'a array
include Binary_searchable.S1 with type 'a t := 'a t

See Binary_search.binary_search in binary_search.ml

See Binary_search.binary_search_segmented in binary_search.ml

Sourceval binary_search_segmented : ?pos:int -> ?len:int -> 'a t -> segment_of:('a -> [ `Left | `Right ]) @ local -> [ `Last_on_left | `First_on_right ] -> int option @ local
Sourceval array_should_be_polymorphic_over_value_or_null : unit

Refer to this in code that hacks around array's current lack of value_or_null mod non_float support. When the appropriate compiler features land, we will remove this binding and fix up relevant client code.

include Indexed_container.S1_with_creators__''base_or_null_with_ext'' with type ('a : value_or_null mod separable) t := 'a t with type ('a : bits64 mod separable) t__bits64 = 'a t and type ('a : bits32 mod separable) t__bits32 = 'a t and type ('a : word mod separable) t__word = 'a t and type ('a : float64 mod separable) t__float64 = 'a t and type ('a : float32 mod separable) t__float32 = 'a t and type ('a : (value_or_null mod external64) mod separable) t__'value_or_null_mod_external64' = 'a t
include Container.S1__''base_or_null_with_ext'' with type ('a : value_or_null mod separable) t := 'a t with type ('a : bits64 mod separable) t__bits64 = 'a t with type ('a : bits32 mod separable) t__bits32 = 'a t with type ('a : word mod separable) t__word = 'a t with type ('a : float64 mod separable) t__float64 = 'a t with type ('a : float32 mod separable) t__float32 = 'a t with type ('a : (value_or_null mod external64) mod separable) t__'value_or_null_mod_external64' = 'a t
Sourcetype ('a : bits64 mod separable) t__bits64 = 'a t
Sourceand ('a : bits32 mod separable) t__bits32 = 'a t
Sourceand ('a : word mod separable) t__word = 'a t
Sourceand ('a : float64 mod separable) t__float64 = 'a t
Sourceand ('a : float32 mod separable) t__float32 = 'a t
Sourceand ('a : (value_or_null mod external64) mod separable) t__'value_or_null_mod_external64' = 'a t
include sig ... end
include sig ... end

These are all like their equivalents in Container except that an index starting at 0 is added as the first argument to f.

include Container.S1_with_creators__''base_or_null_with_ext'' with type ('a : bits64 mod separable) t__bits64 := 'a t__bits64 and type ('a : bits32 mod separable) t__bits32 := 'a t__bits32 and type ('a : word mod separable) t__word := 'a t__word and type ('a : float64 mod separable) t__float64 := 'a t__float64 and type ('a : float32 mod separable) t__float32 := 'a t__float32 and type ('a : value_or_null mod separable) t := 'a t and type ('a : (value_or_null mod external64) mod separable) t__'value_or_null_mod_external64' := 'a t__'value_or_null_mod_external64'
include sig ... end
include sig ... end
include sig ... end
include sig ... end
include sig ... end
include sig ... end
include sig ... end
include sig ... end
include sig ... end
include sig ... end
include sig ... end
Sourceval compare : ('a : value_or_null mod separable). ('a -> 'a -> int) -> 'a t -> 'a t -> int
Sourceval compare__local : ('a : value_or_null mod separable). ('a @ local -> 'a @ local -> int) -> 'a t @ local -> 'a t @ local -> int
Sourceval equal : ('a : value_or_null mod separable). ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
Sourceval equal__local : ('a : value_or_null mod separable). ('a @ local -> 'a @ local -> bool) -> 'a t @ local -> 'a t @ local -> bool
Sourceval sexp_of_t : ('a : value_or_null mod separable). ('a -> Sexplib0.Sexp.t) -> 'a t -> Sexplib0.Sexp.t
Sourceval sexp_of_t__stack : ('a : value_or_null mod separable). ('a @ local -> Sexplib0.Sexp.t @ local) -> 'a t @ local -> Sexplib0.Sexp.t @ local
Sourceval t_of_sexp : ('a : value_or_null mod separable). (Sexplib0.Sexp.t -> 'a) -> Sexplib0.Sexp.t -> 'a t
Sourceval globalize : ('a : value_or_null mod separable). ('a @ local -> 'a) -> 'a t @ local -> 'a t
include sig ... end
Sourceval t_sexp_grammar : ('a : value_or_null mod separable). 'a Sexplib0.Sexp_grammar.t -> 'a t Sexplib0.Sexp_grammar.t @@ portable
include Indexed_container.S1_with_creators__''value_or_null'' with type ('a : value_or_null mod separable) t := 'a t
include Container.S1__''value_or_null'' with type ('a : value_or_null mod separable) t := 'a t
include sig ... end
include sig ... end

These are all like their equivalents in Container except that an index starting at 0 is added as the first argument to f.

Sourceval iteri : ('a : value_or_null mod separable) 'p1 'p2. ('a, 'p1, 'p2) {t}5/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> f: (int -> ('a {elt}6/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> unit) @ local) @ local -> unit
Sourceval existsi : ('a : value_or_null mod separable) 'p1 'p2. ('a, 'p1, 'p2) {t}5/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> f: (int -> ('a {elt}6/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> bool) @ local) @ local -> bool
Sourceval for_alli : ('a : value_or_null mod separable) 'p1 'p2. ('a, 'p1, 'p2) {t}5/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> f: (int -> ('a {elt}6/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> bool) @ local) @ local -> bool
Sourceval counti : ('a : value_or_null mod separable) 'p1 'p2. ('a, 'p1, 'p2) {t}5/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> f: (int -> ('a {elt}6/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> bool) @ local) @ local -> int
Sourceval foldi : ('a : value_or_null mod separable) 'p1 'p2 ('acc : value_or_null). ('a, 'p1, 'p2) {t}5/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> init:'acc -> f: (int -> ('acc -> ('a {elt}6/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> 'acc) @ local) @ local) @ local -> 'acc
Sourceval iteri_until : ('a : value_or_null mod separable) 'p1 'p2 ('final : value_or_null). ('a, 'p1, 'p2) {t}5/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> f: (int -> ('a {elt}6/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> (unit, 'final) Container.Continue_or_stop.t) @ local) @ local -> finish:(int -> 'final) @ local -> 'final
Sourceval find_mapi : ('a : value_or_null mod separable) 'p1 'p2 ('b : value_or_null). ('a, 'p1, 'p2) {t}5/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> f: (int -> ('a {elt}6/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> 'b option) @ local) @ local -> 'b option
Sourceval foldi_until : ('a : value_or_null mod separable) 'p1 'p2 ('acc : value_or_null) ('final : value_or_null). ('a, 'p1, 'p2) {t}5/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> init:'acc -> f: (int -> ('acc -> ('a {elt}6/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> ('acc, 'final) Container.Continue_or_stop.t) @ local) @ local) @ local -> finish:(int -> ('acc -> 'final) @ local) @ local -> 'final
Sourceval findi : ('a : value_or_null mod separable) 'p1 'p2. ('a, 'p1, 'p2) t -> f:(int -> ('a elt -> bool) @ local) @ local -> (int * 'a elt) option
include Container.S1_with_creators__''value_or_null'' with type ('a : value_or_null mod separable) t := 'a t
include sig ... end
Sourceval is_empty : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> bool
Sourceval iter : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> f:('a -> unit) @ local -> unit

iter must allow exceptions raised in f to escape, terminating the iteration cleanly. The same holds for all functions below taking an f.

Sourceval exists : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> f:('a -> bool) @ local -> bool

Returns true if and only if there exists an element for which the provided function evaluates to true. This is a short-circuiting operation.

Sourceval for_all : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> f:('a -> bool) @ local -> bool

Returns true if and only if the provided function evaluates to true for all elements. This is a short-circuiting operation.

Sourceval count : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> f:('a -> bool) @ local -> int

Returns the number of elements for which the provided function evaluates to true.

Sourceval find : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> f:('a -> bool) @ local -> 'a option

Returns as an option the first element for which f evaluates to true.

Sourceval to_list : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> 'a list
Sourceval min_elt : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> compare:('a -> ('a -> int) @ local) @ local -> 'a option

Returns a min (resp. max) element from the collection using the provided compare function. In case of a tie, the first element encountered while traversing the collection is returned. The implementation uses fold so it has the same complexity as fold. Returns None iff the collection is empty.

Sourceval max_elt : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> compare:('a -> ('a -> int) @ local) @ local -> 'a option
Sourceval sum : ('a : value_or_null mod separable) ('sum : value_or_null) 'p1 'p2. (module Container.Summable with type t = 'sum) -> 'a t -> f:('a -> 'sum) @ local -> 'sum

Returns the sum of f i for all i in the container. The order in which the elements will be summed is unspecified.

Sourceval iter_until : ('a : value_or_null mod separable) 'p1 'p2 ('final : value_or_null). 'a t -> f:('a -> (unit, 'final) Container.Continue_or_stop.t) @ local -> finish:(unit -> 'final) @ local -> 'final

iter_until t ~f ~finish is a short-circuiting version of iter. If f returns Stop x the computation ceases and returns x. If f always returns Continue () the final result is computed by finish.

Sourceval fold : ('a : value_or_null mod separable) 'p1 'p2 ('acc : value_or_null). 'a t -> init:'acc -> f:('acc -> ('a -> 'acc) @ local) @ local -> 'acc

fold t ~init ~f returns f (... f (f (f init e1) e2) e3 ...) en, where e1..en are the elements of t.

Sourceval fold_result : ('a : value_or_null mod separable) 'p1 'p2 ('acc : value_or_null) 'e. 'a t -> init:'acc -> f:('acc -> ('a -> ('acc, 'e) result) @ local) @ local -> ('acc, 'e) result

fold_result t ~init ~f is a short-circuiting version of fold that runs in the Result monad. If f returns an Error _, that value is returned without any additional invocations of f.

Sourceval find_map : ('a : value_or_null mod separable) 'p1 'p2 ('b : value_or_null). 'a t -> f:('a -> 'b option) @ local -> 'b option

Returns the first evaluation of f that returns Some, and returns None if there is no such element.

include sig ... end
Sourceval fold_until : ('a : value_or_null mod separable) 'p1 'p2 ('acc : value_or_null) ('final : value_or_null). 'a t -> init:'acc -> f: ('acc -> ('a -> ('acc, 'final) Container.Continue_or_stop.t) @ local) @ local -> finish:('acc -> 'final) @ local -> 'final

fold_until t ~init ~f ~finish is a short-circuiting version of fold. If f returns Stop _ the computation ceases and results in that value. If f returns Continue _, the fold will proceed. If f never returns Stop _, the final result is computed by finish.

Example:

  type maybe_negative =
    | Found_negative of int
    | All_nonnegative of { sum : int }

  (** [first_neg_or_sum list] returns the first negative number in [list], if any,
      otherwise returns the sum of the list. *)
  let first_neg_or_sum =
    List.fold_until ~init:0
      ~f:(fun sum x ->
        if x < 0
        then Stop (Found_negative x)
        else Continue (sum + x))
      ~finish:(fun sum -> All_nonnegative { sum })
  ;;

  let x = first_neg_or_sum [1; 2; 3; 4; 5]
  val x : maybe_negative = All_nonnegative {sum = 15}

  let y = first_neg_or_sum [1; 2; -3; 4; 5]
  val y : maybe_negative = Found_negative -3
Sourceval mem : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> 'a -> equal:('a -> ('a -> bool) @ local) @ local -> bool

Checks whether the provided element is there, using equal.

include sig ... end
Sourceval of_list : ('a : value_or_null mod separable) 'p1 'p2. 'a list -> 'a t
Sourceval append : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> 'a t -> 'a t

E.g., append (of_list [a; b]) (of_list [c; d; e]) is of_list [a; b; c; d; e]

Sourceval filter : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> f:('a -> bool) @ local -> 'a t

filter t ~f returns all the elements of t that satisfy the predicate f.

Sourceval partition_tf : ('a : value_or_null mod separable) 'p1 'p2. 'a t -> f:('a -> bool) @ local -> 'a t * 'a t

partition_tf t ~f returns a pair t1, t2, where t1 is all elements of t that satisfy f, and t2 is all elements of t that do not satisfy f. The "tf" suffix is mnemonic to remind readers that the result is (trues, falses).

include sig ... end
Sourceval filter_map : ('a : value_or_null mod separable) 'p1 'p2 ('b : value_or_null mod separable). 'a t -> f:('a -> 'b option) @ local -> 'b t

filter_map t ~f applies f to every x in t. The result contains every y for which f x returns Some y.

Sourceval concat_map : ('a : value_or_null mod separable) 'p1 'p2 ('b : value_or_null mod separable). 'a t -> f:('a -> 'b t) @ local -> 'b t

concat_map t ~f is equivalent to concat (map t ~f).

include sig ... end
Sourceval partition_map : ('a : value_or_null mod separable) 'p1 'p2 ('b : value_or_null mod separable) ('c : value_or_null mod separable). 'a t -> f:('a -> ('b, 'c) Base__.Either0.t) @ local -> 'b t * 'c t

partition_map t ~f partitions t according to f.

Sourceval to_array : ('a : value_or_null mod separable). 'a t -> 'a array
Sourceval of_array : ('a : value_or_null mod separable). 'a array -> 'a t
Sourceval filteri : ('a : value_or_null mod separable) 'p1 'p2. ('a, 'p1, 'p2) {t}132/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> f: (int -> ('a {elt}133/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> bool) @ local) @ local -> ('a, 'p1, 'p2) {t}132/shadowed/(8f9e3060c4c689c937aa7d214ca25fca)
Sourceval mapi : ('a : value_or_null mod separable) 'p1 'p2 ('b : value_or_null mod separable). ('a, 'p1, 'p2) {t}132/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> f: (int -> ('a {elt}133/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> 'b {elt}133/shadowed/(8f9e3060c4c689c937aa7d214ca25fca)) @ local) @ local -> ('b, 'p1, 'p2) {t}132/shadowed/(8f9e3060c4c689c937aa7d214ca25fca)

mapi is like map. Additionally, it passes in the index of each element as the first argument to the mapped function.

Sourceval filter_mapi : ('a : value_or_null mod separable) 'p1 'p2 ('b : value_or_null mod separable). ('a, 'p1, 'p2) {t}132/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> f: (int -> ('a {elt}133/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> 'b {elt}133/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) option) @ local) @ local -> ('b, 'p1, 'p2) {t}132/shadowed/(8f9e3060c4c689c937aa7d214ca25fca)

filter_mapi is like filter_map. Additionally, it passes in the index of each element as the first argument to the mapped function.

Sourceval concat_mapi : ('a : value_or_null mod separable) 'p1 'p2 ('b : value_or_null mod separable). ('a, 'p1, 'p2) {t}132/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> f: (int -> ('a {elt}133/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> ('b, 'p1, 'p2) {t}132/shadowed/(8f9e3060c4c689c937aa7d214ca25fca)) @ local) @ local -> ('b, 'p1, 'p2) {t}132/shadowed/(8f9e3060c4c689c937aa7d214ca25fca)

concat_mapi t ~f is like concat_map. Additionally, it passes the index as an argument.

Sourceval partition_mapi : ('a : value_or_null mod separable) 'p1 'p2 ('b : value_or_null mod separable) ('c : value_or_null mod separable). ('a, 'p1, 'p2) {t}132/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> f: (int -> ('a {elt}133/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) -> ('b {elt}133/shadowed/(8f9e3060c4c689c937aa7d214ca25fca), 'c {elt}133/shadowed/(8f9e3060c4c689c937aa7d214ca25fca)) Base__.Either0.t) @ local) @ local -> ('b, 'p1, 'p2) {t}132/shadowed/(8f9e3060c4c689c937aa7d214ca25fca) * ('c, 'p1, 'p2) {t}132/shadowed/(8f9e3060c4c689c937aa7d214ca25fca)

partition_mapi t ~f is like partition_map. Additionally, it passes the index as an argument.

include Invariant.S1 with type 'a t := 'a t
Sourceval invariant : ('a -> unit) -> 'a t -> unit
Sourceval map : ('a : value_or_null mod separable) ('b : value_or_null mod separable). 'a t -> f:('a -> 'b) @ local -> 'b t
Sourceval max_length : int

Maximum length of a normal array. The maximum length of a float array is max_length/2 on 32-bit machines and max_length on 64-bit machines.

Sourceval length : ('a : any mod separable). 'a array @ immutable -> int
Sourceval get : ('a : any mod separable). 'a array -> int -> 'a

Array.get a n returns the element number n of array a. The first element has number 0. The last element has number Array.length a - 1. You can also write a.(n) instead of Array.get a n.

Raise Invalid_argument "index out of bounds" if n is outside the range 0 to (Array.length a - 1).

Sourceval get_opt : 'a. 'a array @ local -> int -> 'a option

Like get, but returns None instead of raising.

Sourceval set : ('a : any mod separable). 'a array -> int -> 'a -> unit

Array.set a n x modifies array a in place, replacing element number n with x. You can also write a.(n) <- x instead of Array.set a n x.

Raise Invalid_argument "index out of bounds" if n is outside the range 0 to Array.length a - 1.

Sourceval unsafe_get : ('a : any mod separable). 'a array -> int -> 'a

Unsafe version of get. Can cause arbitrary behavior when used for an out-of-bounds array access.

Sourceval unsafe_set : ('a : any mod separable). 'a array -> int -> 'a -> unit

Unsafe version of set. Can cause arbitrary behavior when used for an out-of-bounds array access.

Sourceval create : ('a : any mod separable). len:int -> 'a -> 'a array @ unique

create ~len x creates an array of length len with the value x populated in each element.

Sourceval create_local : ('a : any mod separable). len:int -> 'a -> 'a array @ local unique

create_local ~len x is like create. It allocates the array on the local stack. The array's elements are still global.

Sourceval magic_create_uninitialized : ('a : any mod separable). len:int -> 'a array

magic_create_uninitialized ~len creates an array of length len with uninitialized elements -- that is, they may contain arbitrary, nondeterministic 'a values. This can be significantly faster than using create.

magic_create_uninitialized can only be used for GC-ignorable arrays not involving tagged immediates and arrays of elements with unboxed number layout. The compiler rejects attempts to use magic_create_uninitialized to produce e.g. an ('a : value) array.

magic_create_uninitialized can break abstraction boundaries and type safety (e.g. by creating phony witnesses to type equality) and so should be used with caution.

Sourceval create_float_uninitialized : len:int -> float t

create_float_uninitialized ~len creates a float array of length len with uninitialized elements -- that is, they may contain arbitrary, nondeterministic float values. This can be significantly faster than using create, when unboxed float array representations are enabled.

Sourceval init : ('a : value_or_null mod separable). int -> f:(int -> 'a) @ local -> 'a array

init n ~f creates an array of length n with index i set to f i.

Sourceval make_matrix : dimx:int -> dimy:int -> 'a -> 'a t t

Array.make_matrix dimx dimy e returns a two-dimensional array (an array of arrays) with first dimension dimx and second dimension dimy. All the elements of this new matrix are initially physically equal to e. The element (x,y) of a matrix m is accessed with the notation m.(x).(y).

Raise Invalid_argument if dimx or dimy is negative or greater than Array.max_length.

If the value of e is a floating-point number, then the maximum size is only Array.max_length / 2.

Sourceval copy_matrix : 'a t t @ local -> 'a t t

Array.copy_matrix t returns a fresh copy of the array of arrays t. This is typically used when t is a matrix created by Array.make_matrix.

Sourceval concat : ('a : value_or_null mod separable). 'a t list @ local -> 'a t

Like Array.append, but concatenates a list of arrays.

Sourceval copy : ('a : value_or_null mod separable). 'a t @ local -> 'a t

Array.copy a returns a copy of a, that is, a fresh array containing the same elements as a.

Sourceval fill : ('a : value_or_null mod separable). 'a t @ local -> pos:int -> len:int -> 'a -> unit

Array.fill a ofs len x modifies the array a in place, storing x in elements number ofs to ofs + len - 1.

Raise Invalid_argument "Array.fill" if ofs and len do not designate a valid subarray of a.

Array.blit v1 o1 v2 o2 len copies len elements from array v1, starting at element number o1, to array v2, starting at element number o2. It works correctly even if v1 and v2 are the same array, and the source and destination chunks overlap.

Raise Invalid_argument "Array.blit" if o1 and len do not designate a valid subarray of v1, or if o2 and len do not designate a valid subarray of v2.

int_blit and float_blit provide fast bound-checked blits for immediate data types. The unsafe versions do not bound-check the arguments.

include Blit.S1__'value_or_null_mod_separable' with type ('a : any) t := 'a t
Sourceval blit : ('a : value_or_null mod separable) 'p1 'p2 'p3 'p4. src:'a t @ local -> src_pos:int -> dst:'a t @ local -> dst_pos:int -> len:int -> unit
Sourceval blito : ('a : value_or_null mod separable) 'p1 'p2 'p3 'p4. src:'a t @ local -> ?src_pos:int -> ?src_len:int -> dst:'a t @ local -> ?dst_pos:int -> unit -> unit
Sourceval subo : ('a : value_or_null mod separable) 'p1 'p2 'p3 'p4. ?pos:int -> ?len:int -> 'a t @ local -> 'a t
Sourceval unsafe_blit : ('a : value_or_null mod separable). ('a array, 'a array) Blit.blit
Sourceval sub : ('a : value_or_null mod separable). ('a array, 'a array) Blit.sub
include sig ... end
Sourceval foldi_right : 'a t @ local -> init:'acc -> f:(int -> 'a -> 'acc -> 'acc) -> 'acc
Sourceval folding_map : 'a t -> init:'acc -> f:('acc -> ('a -> 'acc * 'b) @ local) @ local -> 'b t

folding_map is a version of map that threads an accumulator through calls to f.

Sourceval folding_mapi : 'a t -> init:'acc -> f:(int -> ('acc -> ('a -> 'acc * 'b) @ local) @ local) @ local -> 'b t
Sourceval fold_map : 'a t -> init:'acc -> f:('acc -> ('a -> 'acc * 'b) @ local) @ local -> 'acc * 'b t

Array.fold_map is a combination of Array.fold and Array.map that threads an accumulator through calls to f.

Sourceval fold_mapi : 'a t -> init:'acc -> f:(int -> ('acc -> ('a -> 'acc * 'b) @ local) @ local) @ local -> 'acc * 'b t
Sourceval fold_right : 'a t -> f:('a -> ('acc -> 'acc) @ local) @ local -> init:'acc -> 'acc

Array.fold_right f a ~init computes f a.(0) (f a.(1) ( ... (f a.(n-1) init) ...)), where n is the length of the array a.

All sort functions in this module sort in increasing order by default.

Sourceval sort : ('a : value_or_null mod separable). ?pos:int -> ?len:int -> 'a t @ local -> compare:('a -> ('a -> int) @ local) @ local -> unit

sort uses constant heap space. stable_sort uses linear heap space.

To sort only part of the array, specify pos to be the index to start sorting from and len indicating how many elements to sort.

Sourceval stable_sort : 'a t -> compare:('a -> 'a -> int) -> unit
Sourceval is_sorted : ('a : value_or_null mod separable). 'a t @ local -> compare:('a -> ('a -> int) @ local) @ local -> bool
Sourceval is_sorted_strictly : ('a : value_or_null mod separable). 'a t @ local -> compare:('a -> ('a -> int) @ local) @ local -> bool

is_sorted_strictly xs ~compare iff is_sorted xs ~compare and no two consecutive elements in xs are equal according to compare.

Sourceval merge : ('a : value_or_null mod separable). 'a t -> 'a t -> compare:('a -> ('a -> int) @ local) @ local -> 'a t

Merges two arrays: assuming that a1 and a2 are sorted according to the comparison function compare, merge a1 a2 ~compare will return a sorted array containing all the elements of a1 and a2. If several elements compare equal, the elements of a1 will be before the elements of a2.

include sig ... end
Sourceval partitioni_tf : 'a t -> f:(int -> ('a -> bool) @ local) @ local -> 'a t * 'a t
Sourceval cartesian_product : 'a t -> 'b t -> ('a * 'b) t
Sourceval transpose : 'a t t -> 'a t t option

transpose in the sense of a matrix transpose. It returns None if the arrays are not all the same length.

Sourceval transpose_exn : 'a t t -> 'a t t
Sourceval filter_opt : 'a option t -> 'a t

filter_opt array returns a new array where None entries are omitted and Some x entries are replaced with x. Note that this changes the index at which elements will appear.

Functions with the 2 suffix raise an exception if the lengths of the two given arrays aren't the same.

Sourceval iter2_exn : 'a t -> 'b t -> f:('a -> ('b -> unit) @ local) @ local -> unit
Sourceval map2_exn : 'a t -> 'b t -> f:('a -> ('b -> 'c) @ local) @ local -> 'c t
Sourceval fold2_exn : 'a t -> 'b t -> init:'acc -> f:('acc -> ('a -> ('b -> 'acc) @ local) @ local) @ local -> 'acc
Sourceval map_inplace : 'a t @ local -> f:('a -> 'a) @ local -> unit

Modifies an array in place, applying f to every element of the array

Sourceval find_exn : ('a : value_or_null mod separable). 'a t -> f:('a -> bool) @ local -> 'a

find_exn f t returns the first a in t for which f t.(i) is true. It raises Stdlib.Not_found or Not_found_s if there is no such a.

Sourceval swap : ('a : value_or_null mod separable). 'a t @ local -> int -> int -> unit

swap arr i j swaps the value at index i with that at index j.

Sourceval rev_inplace : ('a : value_or_null mod separable). 'a t @ local -> unit

rev_inplace t reverses t in place.

Sourceval rev : ('a : value_or_null mod separable). 'a t -> 'a t

rev t returns a reversed copy of t

Sourceval of_list_rev : ('a : value_or_null mod separable). 'a list -> 'a t

of_list_rev l converts from list then reverses in place.

Sourceval find_map_exn : ('a : value_or_null mod separable) ('b : value_or_null). 'a t -> f:('a -> 'b option) @ local -> 'b

Returns the first evaluation of f that returns Some. Raises Stdlib.Not_found or Not_found_s if f always returns None.

Sourceval find_mapi_exn : ('a : value_or_null mod separable) ('b : value_or_null). 'a t -> f:(int -> ('a -> 'b option) @ local) @ local -> 'b

find_mapi_exn is like find_map_exn but passes the index as an argument.

Sourceval of_list_map : ('a : value_or_null) ('b : value_or_null mod separable). 'a list -> f:('a -> 'b) @ local -> 'b t

of_list_map l ~f is the same as of_list (List.map l ~f).

Sourceval of_list_mapi : ('a : value_or_null) ('b : value_or_null mod separable). 'a list -> f:(int -> ('a -> 'b) @ local) @ local -> 'b t

of_list_mapi l ~f is the same as of_list (List.mapi l ~f).

Sourceval of_list_rev_map : ('a : value_or_null) ('b : value_or_null mod separable). 'a list -> f:('a -> 'b) @ local -> 'b t

of_list_rev_map l ~f is the same as of_list (List.rev_map l ~f).

Sourceval of_list_rev_mapi : ('a : value_or_null) ('b : value_or_null mod separable). 'a list -> f:(int -> ('a -> 'b) @ local) @ local -> 'b t

of_list_rev_mapi l ~f is the same as of_list (List.rev_mapi l ~f).

Sourceval for_all2_exn : ('a : value_or_null mod separable) ('b : value_or_null mod separable). 'a t -> 'b t -> f:('a -> ('b -> bool) @ local) @ local -> bool

for_all2_exn t1 t2 ~f fails if length t1 <> length t2.

Sourceval exists2_exn : ('a : value_or_null mod separable) ('b : value_or_null mod separable). 'a t -> 'b t -> f:('a -> ('b -> bool) @ local) @ local -> bool

exists2_exn t1 t2 ~f fails if length t1 <> length t2.

Sourceval find_or_null : 'a. 'a t -> f:('a -> bool) @ local -> 'a or_null

Like find but returning or_null instead of option.

Sourceval findi_or_null : 'a. 'a t -> f:(int -> ('a -> bool) @ local) @ local -> (int * 'a) or_null

Like findi but returning or_null instead of option.

include sig ... end
Sourceval findi_exn : 'a t -> f:(int -> ('a -> bool) @ local) @ local -> int * 'a

For backwards compatibility, we return a boxed product for the value-only version of findi_exn (instead of a value & value product)

Sourceval find_consecutive_duplicate : 'a t -> equal:('a -> ('a -> bool) @ local) @ local -> ('a * 'a) option

find_consecutive_duplicate t ~equal returns the first pair of consecutive elements (a1, a2) in t such that equal a1 a2. They are returned in the same order as they appear in t.

Sourceval reduce : 'a t -> f:('a -> ('a -> 'a) @ local) @ local -> 'a option

reduce f [a1; ...; an] is Some (f (... (f (f a1 a2) a3) ...) an). Returns None on the empty array.

Sourceval reduce_exn : 'a t -> f:('a -> ('a -> 'a) @ local) @ local -> 'a
Sourceval permute : ?random_state:Random.State.t -> ?pos:int -> ?len:int -> 'a t @ local -> unit

permute ?random_state ?pos ?len t randomly permutes t in place.

To permute only part of the array, specify pos to be the index to start permuting from and len indicating how many elements to permute.

permute side-effects random_state by repeated calls to Random.State.int. If random_state is not supplied, permute uses Random.State.default.

Sourceval random_element : ?random_state:Random.State.t -> 'a t -> 'a option

random_element ?random_state t is None if t is empty, else it is Some x for some x chosen uniformly at random from t.

random_element side-effects random_state by calling Random.State.int. If random_state is not supplied, random_element uses Random.State.default.

Sourceval random_element_exn : ?random_state:Random.State.t -> 'a t -> 'a
Sourceval split_n : 'a t -> int -> 'a t * 'a t

split_n t n returns a pair of arrays (first, second) where first contains the first n elements of t and second contains the remaining elements.

  • If n >= length t, returns (t, [||]).
  • If n <= 0, returns ([||], t).
Sourceval chunks_of : 'a t -> length:int -> 'a t t

chunks_of t ~length returns an array of arrays whose concatenation is equal to the original array. Every array has length elements, except for possibly the last array, which may have fewer. chunks_of raises if length <= 0.

Sourceval zip : 'a t -> 'b t -> ('a * 'b) t option

zip is like List.zip, but for arrays.

Sourceval zip_exn : 'a t -> 'b t -> ('a * 'b) t
Sourceval unzip : ('a * 'b) t -> 'a t * 'b t

unzip is like List.unzip, but for arrays.

Sourceval sorted_copy : 'a t @ local -> compare:('a -> ('a -> int) @ local) @ local -> 'a t

sorted_copy ar compare returns a shallow copy of ar that is sorted. Similar to List.sort

Sourceval last : 'a t -> 'a
  • deprecated [since 2024-07] This was renamed to [last_exn]
Sourceval last_exn : 'a t -> 'a
Sourceval to_sequence : 'a t -> 'a Sequence.t

The input array is copied internally so that future modifications of it do not change the sequence.

Sourceval to_sequence_mutable : 'a t -> 'a Sequence.t

The input array is shared with the sequence and modifications of it will result in modification of the sequence.