Module Base__List
Immutable, singly-linked lists, giving fast access to the front of the list, and slow (i.e., O(n)) access to the back of the list. The comparison functions on lists are lexicographic.
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> intval hash_fold_t : (Base.Hash.state -> 'a -> Base.Hash.state) -> Base.Hash.state -> 'a t -> Base.Hash.state
include Base.Sexpable.S1 with type 'a t := 'a t
val t_of_sexp : (Sexplib0.Sexp.t -> 'a) -> Sexplib0.Sexp.t -> 'a tval sexp_of_t : ('a -> Sexplib0.Sexp.t) -> 'a t -> Sexplib0.Sexp.t
val t_sexp_grammar : Base.Sexp.Private.Raw_grammar.t
include Base.Container.S1 with type 'a t := 'a t
val mem : 'a t -> 'a -> equal:('a -> 'a -> bool) -> boolChecks whether the provided element is there, using
equal.
val length : 'a t -> intval is_empty : 'a t -> boolval iter : 'a t -> f:('a -> unit) -> unitval fold : 'a t -> init:'accum -> f:('accum -> 'a -> 'accum) -> 'accumfold t ~init ~freturnsf (... f (f (f init e1) e2) e3 ...) en, wheree1..enare the elements oft
val fold_result : 'a t -> init:'accum -> f:('accum -> 'a -> ('accum, 'e) Base.Result.t) -> ('accum, 'e) Base.Result.tfold_result t ~init ~fis a short-circuiting version offoldthat runs in theResultmonad. Iffreturns anError _, that value is returned without any additional invocations off.
val fold_until : 'a t -> init:'accum -> f:('accum -> 'a -> ('accum, 'final) Base__.Container_intf.Continue_or_stop.t) -> finish:('accum -> 'final) -> 'finalfold_until t ~init ~f ~finishis a short-circuiting version offold. IffreturnsStop _the computation ceases and results in that value. IffreturnsContinue _, the fold will proceed. Iffnever returnsStop _, the final result is computed byfinish.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
val exists : 'a t -> f:('a -> bool) -> boolReturns
trueif and only if there exists an element for which the provided function evaluates totrue. This is a short-circuiting operation.
val for_all : 'a t -> f:('a -> bool) -> boolReturns
trueif and only if the provided function evaluates totruefor all elements. This is a short-circuiting operation.
val count : 'a t -> f:('a -> bool) -> intReturns the number of elements for which the provided function evaluates to true.
val sum : (module Base__.Container_intf.Summable with type t = 'sum) -> 'a t -> f:('a -> 'sum) -> 'sumReturns the sum of
f ifor alliin the container.
val find : 'a t -> f:('a -> bool) -> 'a optionReturns as an
optionthe first element for whichfevaluates to true.
val find_map : 'a t -> f:('a -> 'b option) -> 'b optionReturns the first evaluation of
fthat returnsSome, and returnsNoneif there is no such element.
val to_list : 'a t -> 'a listval to_array : 'a t -> 'a arrayval min_elt : 'a t -> compare:('a -> 'a -> int) -> 'a optionReturns a minimum (resp maximum) element from the collection using the provided
comparefunction, orNoneif the collection is empty. In case of a tie, the first element encountered while traversing the collection is returned. The implementation usesfoldso it has the same complexity asfold.
val max_elt : 'a t -> compare:('a -> 'a -> int) -> 'a option
include Base__.Invariant_intf.S1 with type 'a t := 'a t
val invariant : 'a Base__.Invariant_intf.inv -> 'a t Base__.Invariant_intf.inv
include Base.Monad.S with type 'a t := 'a t
include Base__.Monad_intf.S_without_syntax with type 'a t := 'a t
module Monad_infix : Base__.Monad_intf.Infix with type 'a t := 'a tval return : 'a -> 'a treturn vreturns the (trivial) computation that returns v.
module Or_unequal_lengths : sig ... endOr_unequal_lengthsis used for functions that take multiple lists and that only make sense if all the lists have the same length, e.g.,iter2,map3. Such functions check the list lengths prior to doing anything else, and returnUnequal_lengthsif not all the lists have the same length.
val of_list : 'a t -> 'a tof_listis the identity function. It is useful so that theListmodule matches the same signature that other container modules do, namely:val of_list : 'a List.t -> 'a t
val nth : 'a t -> int -> 'a optionval nth_exn : 'a t -> int -> 'aReturn the
n-th element of the given list. The first element (head of the list) is at position 0. Raise if the list is too short ornis negative.
val rev_append : 'a t -> 'a t -> 'a trev_append l1 l2reversesl1and concatenates it tol2. This is equivalent to(List.revl1) @ l2, butrev_appendis more efficient.
val unordered_append : 'a t -> 'a t -> 'a tunordered_append l1 l2has the same elements asl1 @ l2, but in some unspecified order. Generally takes time proportional to length of first list, but is O(1) if either list is empty.
val rev_map : 'a t -> f:('a -> 'b) -> 'b trev_map l ~fgives the same result asList.rev(ListLabels.mapf l), but is more efficient.
val iter2_exn : 'a t -> 'b t -> f:('a -> 'b -> unit) -> unititer2 [a1; ...; an] [b1; ...; bn] ~fcalls in turnf a1 b1; ...; f an bn. The exn version will raise if the two lists have different lengths.
val iter2 : 'a t -> 'b t -> f:('a -> 'b -> unit) -> unit Or_unequal_lengths.tval rev_map2_exn : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c trev_map2_exn l1 l2 ~fgives the same result asList.rev (List.map2_exn l1 l2 ~f), but is more efficient.
val rev_map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t Or_unequal_lengths.tval fold2_exn : 'a t -> 'b t -> init:'c -> f:('c -> 'a -> 'b -> 'c) -> 'cfold2 ~f ~init:a [b1; ...; bn] [c1; ...; cn]isf (... (f (f a b1 c1) b2 c2) ...) bn cn. The exn version will raise if the two lists have different lengths.
val fold2 : 'a t -> 'b t -> init:'c -> f:('c -> 'a -> 'b -> 'c) -> 'c Or_unequal_lengths.tval for_alli : 'a t -> f:(int -> 'a -> bool) -> boolLike
List.for_all, but passes the index as an argument.
val for_all2_exn : 'a t -> 'b t -> f:('a -> 'b -> bool) -> boolLike
List.for_all, but for a two-argument predicate. The exn version will raise if the two lists have different lengths.
val for_all2 : 'a t -> 'b t -> f:('a -> 'b -> bool) -> bool Or_unequal_lengths.tval existsi : 'a t -> f:(int -> 'a -> bool) -> boolLike
List.exists, but passes the index as an argument.
val exists2_exn : 'a t -> 'b t -> f:('a -> 'b -> bool) -> boolLike
List.exists, but for a two-argument predicate. The exn version will raise if the two lists have different lengths.
val exists2 : 'a t -> 'b t -> f:('a -> 'b -> bool) -> bool Or_unequal_lengths.tval filter : 'a t -> f:('a -> bool) -> 'a tfilter l ~freturns all the elements of the listlthat satisfy the predicatef. The order of the elements in the input list is preserved.
val rev_filter : 'a t -> f:('a -> bool) -> 'a tLike
filter, but reverses the order of the input list.
val filteri : 'a t -> f:(int -> 'a -> bool) -> 'a tval partition_map : 'a t -> f:('a -> ('b, 'c) Base__.Either0.t) -> 'b t * 'c tpartition_map t ~fpartitionstaccording tof.
val partition3_map : 'a t -> f:('a -> [ `Fst of 'b | `Snd of 'c | `Trd of 'd ]) -> 'b t * 'c t * 'd tval partition_tf : 'a t -> f:('a -> bool) -> 'a t * 'a tpartition_tf l ~freturns a pair of lists(l1, l2), wherel1is the list of all the elements oflthat satisfy the predicatep, andl2is the list of all the elements oflthat do not satisfyp. The order of the elements in the input list is preserved. The "tf" suffix is mnemonic to remind readers at a call that the result is (trues, falses).
val partition_result : ('ok, 'error) Base.Result.t t -> 'ok t * 'error tpartition_result lreturns a pair of lists(l1, l2), wherel1is the list of allOkelements inlandl2is the list of allErrorelements. The order of elements in the input list is preserved.
val split_n : 'a t -> int -> 'a t * 'a tsplit_n [e1; ...; em] nis([e1; ...; en], [en+1; ...; em]).- If
n > m,([e1; ...; em], [])is returned. - If
n < 0,([], [e1; ...; em])is returned.
- If
val sort : 'a t -> compare:('a -> 'a -> int) -> 'a tSort a list in increasing order according to a comparison function. The comparison function must return 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller (see
Array.sortfor a complete specification). For example,Poly.compare is a suitable comparison function.The current implementation uses Merge Sort. It runs in linear heap space and logarithmic stack space.
Presently, the sort is stable, meaning that two equal elements in the input will be in the same order in the output.
val merge : 'a t -> 'a t -> compare:('a -> 'a -> int) -> 'a tMerges two lists: assuming that
l1andl2are sorted according to the comparison functioncompare,merge compare l1 l2will return a sorted list containing all the elements ofl1andl2. If several elements compare equal, the elements ofl1will be before the elements ofl2.
val hd : 'a t -> 'a optionval tl : 'a t -> 'a t optionval hd_exn : 'a t -> 'aReturns the first element of the given list. Raises if the list is empty.
val tl_exn : 'a t -> 'a tReturns the given list without its first element. Raises if the list is empty.
val findi : 'a t -> f:(int -> 'a -> bool) -> (int * 'a) optionval find_exn : 'a t -> f:('a -> bool) -> 'afind_exn t ~freturns the first element oftthat satisfiesf. It raisesCaml.Not_foundorNot_found_sif there is no such element.
val find_map_exn : 'a t -> f:('a -> 'b option) -> 'bReturns the first evaluation of
fthat returnsSome. RaisesCaml.Not_foundorNot_found_siffalways returnsNone.
val find_mapi : 'a t -> f:(int -> 'a -> 'b option) -> 'b optionval find_mapi_exn : 'a t -> f:(int -> 'a -> 'b option) -> 'bval append : 'a t -> 'a t -> 'a tE.g.,
append [1; 2] [3; 4; 5]is[1; 2; 3; 4; 5]
val map : 'a t -> f:('a -> 'b) -> 'b tmap f [a1; ...; an]applies functionftoa1,a2, ...,an, in order, and builds the list[f a1; ...; f an]with the results returned byf.
val folding_map : 'a t -> init:'b -> f:('b -> 'a -> 'b * 'c) -> 'c tval folding_mapi : 'a t -> init:'b -> f:(int -> 'b -> 'a -> 'b * 'c) -> 'c t
val fold_map : 'a t -> init:'b -> f:('b -> 'a -> 'b * 'c) -> 'b * 'c tval fold_mapi : 'a t -> init:'b -> f:(int -> 'b -> 'a -> 'b * 'c) -> 'b * 'c tval concat_map : 'a t -> f:('a -> 'b t) -> 'b tconcat_map t ~fisconcat (map t ~f), except that there is no guarantee about the order in whichfis applied to the elements oft.
val concat_mapi : 'a t -> f:(int -> 'a -> 'b t) -> 'b tconcat_mapi t ~fis like concat_map, but passes the index as an argument
val map2_exn : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c tmap2 [a1; ...; an] [b1; ...; bn] ~fis[f a1 b1; ...; f an bn]. The exn version will raise if the two lists have different lengths.
val map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t Or_unequal_lengths.t
val rev_map3_exn : 'a t -> 'b t -> 'c t -> f:('a -> 'b -> 'c -> 'd) -> 'd tval rev_map3 : 'a t -> 'b t -> 'c t -> f:('a -> 'b -> 'c -> 'd) -> 'd t Or_unequal_lengths.t
val map3_exn : 'a t -> 'b t -> 'c t -> f:('a -> 'b -> 'c -> 'd) -> 'd tval map3 : 'a t -> 'b t -> 'c t -> f:('a -> 'b -> 'c -> 'd) -> 'd t Or_unequal_lengths.tval rev_map_append : 'a t -> 'b t -> f:('a -> 'b) -> 'b trev_map_append l1 l2 ~freversesl1mappingfover each element, and appends the result to the front ofl2.
val fold_right : 'a t -> f:('a -> 'b -> 'b) -> init:'b -> 'bfold_right [a1; ...; an] ~f ~init:bisf a1 (f a2 (... (f an b) ...)).
val fold_left : 'a t -> init:'b -> f:('b -> 'a -> 'b) -> 'bfold_leftis the same asContainer.S1.fold, and one should always usefoldrather thanfold_left, except in functors that are parameterized over a more general signature where this equivalence does not hold.
val zip : 'a t -> 'b t -> ('a * 'b) t Or_unequal_lengths.tval zip_exn : 'a t -> 'b t -> ('a * 'b) tval mapi : 'a t -> f:(int -> 'a -> 'b) -> 'b tmapiis just like map, but it also passes in the index of each element as the first argument to the mapped function. Tail-recursive.
val rev_mapi : 'a t -> f:(int -> 'a -> 'b) -> 'b tval iteri : 'a t -> f:(int -> 'a -> unit) -> unititeriis just likeiter, but it also passes in the index of each element as the first argument to the iter'd function. Tail-recursive.
val foldi : 'a t -> init:'b -> f:(int -> 'b -> 'a -> 'b) -> 'bfoldiis just likefold, but it also passes in the index of each element as the first argument to the folded function. Tail-recursive.
val reduce_exn : 'a t -> f:('a -> 'a -> 'a) -> 'areduce_exn [a1; ...; an] ~fisf (... (f (f a1 a2) a3) ...) an. It fails on the empty list. Tail recursive.
val reduce : 'a t -> f:('a -> 'a -> 'a) -> 'a optionval reduce_balanced : 'a t -> f:('a -> 'a -> 'a) -> 'a optionreduce_balancedreturns the same value asreducewhenfis associative, but differs in that the tree of nested applications offhas logarithmic depth.This is useful when your
'agrows in size as you reduce it andfbecomes more expensive with bigger inputs. For example,reduce_balanced ~f:(^)takesn*log(n)time, whilereduce ~f:(^)takes quadratic time.
val reduce_balanced_exn : 'a t -> f:('a -> 'a -> 'a) -> 'aval group : 'a t -> break:('a -> 'a -> bool) -> 'a t tgroup l ~breakreturns a list of lists (i.e., groups) whose concatenation is equal to the original list. Each group is broken wherebreakreturns true on a pair of successive elements.Example:
group ~break:(<>) ['M';'i';'s';'s';'i';'s';'s';'i';'p';'p';'i'] -> [['M'];['i'];['s';'s'];['i'];['s';'s'];['i'];['p';'p'];['i']]
val groupi : 'a t -> break:(int -> 'a -> 'a -> bool) -> 'a t tThis is just like
group, except that you get the index in the original list of the current element along with the two elements.Example, group the chars of
"Mississippi"into triples:groupi ~break:(fun i _ _ -> i mod 3 = 0) ['M';'i';'s';'s';'i';'s';'s';'i';'p';'p';'i'] -> [['M'; 'i'; 's']; ['s'; 'i'; 's']; ['s'; 'i'; 'p']; ['p'; 'i']]
val chunks_of : 'a t -> length:int -> 'a t tchunks_of l ~lengthreturns a list of lists whose concatenation is equal to the original list. Every list haslengthelements, except for possibly the last list, which may have fewer.chunks_ofraises iflength <= 0.
val last : 'a t -> 'a optionThe final element of a list. The
_exnversion raises on the empty list.
val last_exn : 'a t -> 'aval is_prefix : 'a t -> prefix:'a t -> equal:('a -> 'a -> bool) -> boolis_prefix xs ~prefixreturnstrueifxsstarts withprefix.
val is_suffix : 'a t -> suffix:'a t -> equal:('a -> 'a -> bool) -> boolis_suffix xs ~suffixreturnstrueifxsends withsuffix.
val find_consecutive_duplicate : 'a t -> equal:('a -> 'a -> bool) -> ('a * 'a) optionfind_consecutive_duplicate t ~equalreturns the first pair of consecutive elements(a1, a2)intsuch thatequal a1 a2. They are returned in the same order as they appear int.equalneed not be an equivalence relation; it is simply used as a predicate on consecutive elements.
val remove_consecutive_duplicates : ?which_to_keep:[ `First | `Last ] -> 'a t -> equal:('a -> 'a -> bool) -> 'a tReturns the given list with consecutive duplicates removed. The relative order of the other elements is unaffected. The element kept from a run of duplicates is determined by
which_to_keep.
val dedup_and_sort : compare:('a -> 'a -> int) -> 'a t -> 'a tReturns the given list with duplicates removed and in sorted order.
val find_a_dup : compare:('a -> 'a -> int) -> 'a t -> 'a optionfind_a_dupreturns a duplicate from the list (with no guarantees about which duplicate you get), orNoneif there are no dups.
val contains_dup : compare:('a -> 'a -> int) -> 'a t -> boolReturns true if there are any two elements in the list which are the same. O(n log n) time complexity.
val find_all_dups : compare:('a -> 'a -> int) -> 'a t -> 'a listfind_all_dupsreturns a list of all elements that occur more than once, with no guarantees about order. O(n log n) time complexity.
val count : 'a t -> f:('a -> bool) -> intcount l ~fis the number of elements inlthat satisfy the predicatef.
val counti : 'a t -> f:(int -> 'a -> bool) -> intval range : ?stride:int -> ?start:[ `inclusive | `exclusive ] -> ?stop:[ `inclusive | `exclusive ] -> int -> int -> int trange ?stride ?start ?stop start_i stop_iis the list of integers fromstart_itostop_i, stepping bystride. Ifstride< 0 then we needstart_i>stop_ifor the result to be nonempty (orstart_i=stop_iin the case where both bounds are inclusive).
val range' : compare:('a -> 'a -> int) -> stride:('a -> 'a) -> ?start:[ `inclusive | `exclusive ] -> ?stop:[ `inclusive | `exclusive ] -> 'a -> 'a -> 'a trange'is analogous torangefor general start/stop/stride types.range'raises ifstride xreturnsxor if the direction thatstride xmovesxchanges from one call to the next.
val init : int -> f:(int -> 'a) -> 'a tinit n ~fis[(f 0); (f 1); ...; (f (n-1))]. It is an error ifn < 0.initappliesfto values in decreasing order; starting withn - 1, and ending with0. This is the opposite order toArray.init.
val rev_filter_map : 'a t -> f:('a -> 'b option) -> 'b trev_filter_map l ~fis the reversed sublist oflcontaining only elements for whichfreturnsSome e.
val rev_filter_mapi : 'a t -> f:(int -> 'a -> 'b option) -> 'b trev_filter_mapi is just like
rev_filter_map, but it also passes in the index of each element as the first argument to the mapped function. Tail-recursive.
val filter_map : 'a t -> f:('a -> 'b option) -> 'b tfilter_map l ~fis the sublist oflcontaining only elements for whichfreturnsSome e.
val filter_mapi : 'a t -> f:(int -> 'a -> 'b option) -> 'b tfilter_mapi is just like
filter_map, but it also passes in the index of each element as the first argument to the mapped function. Tail-recursive.
val filter_opt : 'a option t -> 'a tfilter_opt lis the sublist oflcontaining only elements which areSome e. In other words,filter_opt l=filter_map ~f:Fn.id l.
module Assoc : sig ... endInterpret a list of (key, value) pairs as a map in which only the first occurrence of a key affects the semantics, i.e.:
val sub : 'a t -> pos:int -> len:int -> 'a tsub pos len lis thelen-element sublist ofl, starting atpos.
val take : 'a t -> int -> 'a ttake l nreturns the firstnelements ofl, or all oflifn > length l.take l n = fst (split_n l n).
val drop : 'a t -> int -> 'a tdrop l nreturnslwithout the firstnelements, or the empty list ifn > length l.drop l nis equivalent tosnd (split_n l n).
val take_while : 'a t -> f:('a -> bool) -> 'a ttake_while l ~freturns the longest prefix oflfor whichfistrue.
val drop_while : 'a t -> f:('a -> bool) -> 'a tdrop_while l ~fdrops the longest prefix oflfor whichfistrue.
val split_while : 'a t -> f:('a -> bool) -> 'a t * 'a tsplit_while xs ~f = (take_while xs ~f, drop_while xs ~f).
val drop_last : 'a t -> 'a t optiondrop_last ldrops the last element ofl, returningNoneiflisempty.
val drop_last_exn : 'a t -> 'a tval concat : 'a t t -> 'a tConcatenates a list of lists. The elements of the argument are all concatenated together (in the same order) to give the result. Tail recursive over outer and inner lists.
val concat_no_order : 'a t t -> 'a tLike
concat, but faster and without preserving any ordering (i.e., for lists that are essentially viewed as multi-sets).
val cons : 'a -> 'a t -> 'a tval cartesian_product : 'a t -> 'b t -> ('a * 'b) tReturns a list with all possible pairs -- if the input lists have length
len1andlen2, the resulting list will have lengthlen1 * len2.
val permute : ?random_state:Base.Random.State.t -> 'a t -> 'a tpermute ?random_state treturns a permutation oft.permuteside-effectsrandom_stateby repeated calls toRandom.State.int. Ifrandom_stateis not supplied,permuteusesRandom.State.default.
val random_element : ?random_state:Base.Random.State.t -> 'a t -> 'a optionrandom_element ?random_state tisNoneiftis empty, else it isSome xfor somexchosen uniformly at random fromt.random_elementside-effectsrandom_stateby callingRandom.State.int. Ifrandom_stateis not supplied,random_elementusesRandom.State.default.
val random_element_exn : ?random_state:Base.Random.State.t -> 'a t -> 'aval is_sorted : 'a t -> compare:('a -> 'a -> int) -> boolis_sorted t ~comparereturnstrueiff for all adjacenta1; a2int,compare a1 a2 <= 0.is_sorted_strictlyis similar, except it uses<instead of<=.
val is_sorted_strictly : 'a t -> compare:('a -> 'a -> int) -> boolval equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
module Infix : sig ... endval transpose : 'a t t -> 'a t t optiontranspose mtransposes the rows and columns of the matrixm, considered as either a row of column lists or (dually) a column of row lists.Example:
transpose [[1;2;3];[4;5;6]] = [[1;4];[2;5];[3;6]]On non-empty rectangular matrices,
transposeis an involution (i.e.,transpose (transpose m) = m). Transpose returnsNonewhen called on lists of lists with non-uniform lengths.