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).
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.
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
Returns a minimum (resp maximum) element from the collection using the provided compare function, or None if the collection is empty. 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.
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).
create_local ~len x is like create. It allocates the array on the local stack. The array's elements are still global.
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.
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.
Array.copy a returns a copy of a, that is, a fresh array containing the same elements as a.
Sourceval fill : 'at->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.
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.
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 : 'at->'bt->f:('a->'b-> unit)-> unit
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.
The input array is shared with the sequence and modifications of it will result in modification of the sequence.
Extensions
We add extensions for Int and Float arrays to make them bin-able, comparable, sexpable, and blit-able (via Blit.S). Permissioned provides fine-grained access control for arrays.
Operations supporting "normalized" indexes are also available.
normalize array index returns a new index into the array such that if the index is less than zero, the returned index will "wrap around" -- i.e., array.(normalize array (-1)) returns the last element of the array.
slice t start stop returns a new array including elements t.(start) through t.(stop-1), normalized Python-style with the exception that stop = 0 is treated as stop = length t.
The Permissioned module gives the ability to restrict permissions on an array, so you can give a function read-only access to an array, create an immutable array, etc.