Legend:
Library
Module
Module type
Parameter
Class
Class type
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.
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).
val partition_map : 'at->f:('a->('b, 'c)Base__.Either0.t)->'bt * 'ct
val fold : 'at->init:'acc->f:('acc->'a->'acc)->'acc
fold t ~init ~f returns f (... f (f (f init e1) e2) e3 ...) en, where e1..en are the elements of t
val fold_result :
'at->init:'acc->f:('acc->'a->('acc, 'e)Result.t)->('acc, 'e)Result.t
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.
val fold_until :
'at->init:'acc->f:('acc->'a->('acc, 'final)Container.Continue_or_stop.t)->finish:('acc->'final)->'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
val min_elt : 'at->compare:('a->'a-> int)->'a option
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.
val max_elt : 'at->compare:('a->'a-> int)->'a option
These are all like their equivalents in Container except that an index starting at 0 is added as the first argument to f.
t >>= f returns a computation that sequences the computations represented by two monad elements. The resulting computation first does t to yield a value v, and then runs the computation returned by f v.
ignore_m t is map t ~f:(fun _ -> ()). ignore_m used to be called ignore, but we decided that was a bad name, because it shadowed the widely used Stdlib.ignore. Some monads still do let ignore = ignore_m for historical reasons.
Or_unequal_lengths is 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 return Unequal_lengths if not all the lists have the same length.
unordered_append l1 l2 has the same elements as l1 @ 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 fold2_exn :
'at->'bt->init:'acc->f:('acc->'a->'b->'acc)->'acc
fold2 ~f ~init:a [b1; ...; bn] [c1; ...; cn] is f (... (f (f a b1 c1) b2 c2)
...) bn cn. The exn version will raise if the two lists have different lengths.
val fold_right2_exn :
'at->'bt->f:('a->'b->'acc->'acc)->init:'acc->'acc
fold_right2 ~f [a1; ...; an] [b1; ...; bn] ~init:c is f a1 b1 (f a2 b2 (... (f an bn c) ...)). The exn version will raise if the two lists have different lengths.
Like filter, but reverses the order of the input list.
val partition3_map :
'at->f:('a->[ `Fst of 'b| `Snd of 'c| `Trd of 'd ])->'bt * 'ct * 'dt
val partition_result : ('ok, 'error)Result.tt->'okt * 'errort
partition_result l returns a pair of lists (l1, l2), where l1 is the list of all Ok elements in l and l2 is the list of all Error elements. The order of elements in the input list is preserved.
Sort 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.sort for 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 stable_sort : 'at->compare:('a->'a-> int)->'at
Merges two lists: assuming that l1 and l2 are sorted according to the comparison function compare, merge compare l1 l2 will return a sorted list containing all the elements of l1 and l2. If several elements compare equal, the elements of l1 will be before the elements of l2.
rev_map_append l1 l2 ~f reverses l1 mapping f over each element, and appends the result to the front of l2.
val fold_right : 'at->f:('a->'acc->'acc)->init:'acc->'acc
fold_right [a1; ...; an] ~f ~init:b is f a1 (f a2 (... (f an b) ...)).
val fold_left : 'at->init:'acc->f:('acc->'a->'acc)->'acc
fold_left is the same as Container.S1.fold, and one should always use fold rather than fold_left, except in functors that are parameterized over a more general signature where this equivalence does not hold.
Transform a list of pairs into a pair of lists: unzip [(a1,b1); ...; (an,bn)] is ([a1; ...; an], [b1; ...; bn]).
Transform a pair of lists into an (optional) list of pairs: zip [a1; ...; an] [b1;
...; bn] is [(a1,b1); ...; (an,bn)]. Returns Unequal_lengths if the two lists have different lengths.
val reduce_balanced : 'at->f:('a->'a->'a)->'a option
reduce_balanced returns the same value as reduce when f is associative, but differs in that the tree of nested applications of f has logarithmic depth.
This is useful when your 'a grows in size as you reduce it and f becomes more expensive with bigger inputs. For example, reduce_balanced ~f:(^) takes n*log(n) time, while reduce ~f:(^) takes quadratic time.
group l ~break returns a list of lists (i.e., groups) whose concatenation is equal to the original list. Each group is broken where break returns 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 : 'at->break:(int ->'a->'a-> bool)->'att
This 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 sort_and_group : 'at->compare:('a->'a-> int)->'att
Group equal elements into the same buckets. Sorting is stable.
chunks_of l ~length returns a list of lists whose concatenation is equal to the original list. Every list has length elements, except for possibly the last list, which may have fewer. chunks_of raises if length <= 0.
val is_prefix : 'at->prefix:'at->equal:('a->'a-> bool)-> bool
is_prefix xs ~prefix returns true if xs starts with prefix.
val is_suffix : 'at->suffix:'at->equal:('a->'a-> bool)-> bool
is_suffix xs ~suffix returns true if xs ends with suffix.
val find_consecutive_duplicate :
'at->equal:('a->'a-> bool)->('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. equal need not be an equivalence relation; it is simply used as a predicate on consecutive elements.
val remove_consecutive_duplicates :
?which_to_keep:[ `First | `Last ]->'at->equal:('a->'a-> bool)->'at
Returns 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 : 'at->compare:('a->'a-> int)->'at
Returns the given list with duplicates removed and in sorted order. Of duplicates in the original list, the element occurring last in the original list is kept.
val stable_dedup : 'at->compare:('a->'a-> int)->'at
Returns the original list, dropping all occurrences of duplicates after the first.
val find_a_dup : 'at->compare:('a->'a-> int)->'a option
find_a_dup returns a duplicate from the list (with no guarantees about which duplicate you get), or None if there are no dups.
val contains_dup : 'at->compare:('a->'a-> int)-> bool
Returns true if there are any two elements in the list which are the same. O(n log n) time complexity.
val find_all_dups : 'at->compare:('a->'a-> int)->'a list
find_all_dups returns a list of all elements that occur more than once, with no guarantees about order. O(n log n) time complexity.
val all_equal : 'at->equal:('a->'a-> bool)->'a option
all_equal returns a single element of the list that is equal to all other elements, or None if no such element exists.
val range :
?stride:int ->?start:[ `inclusive | `exclusive ]->?stop:[ `inclusive | `exclusive ]->int ->int ->int t
range ?stride ?start ?stop start_i stop_i is the list of integers from start_i to stop_i, stepping by stride. If stride < 0 then we need start_i > stop_i for the result to be nonempty (or start_i = stop_i in the case where both bounds are inclusive).
range' is analogous to range for general start/stop/stride types. range' raises if stride x returns x or if the direction that stride x moves x changes from one call to the next.
rev_filter_map l ~f is the reversed sublist of l containing only elements for which f returns Some e.
val rev_filter_mapi : 'at->f:(int ->'a->'b option)->'bt
rev_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.
drop l n returns l without the first n elements, or the empty list if n >
length l. drop l n is equivalent to snd (split_n l n). If n <= 0, returns l rather than a copy.