include Bare_sigs.List.S
Trivial values
type 'a t = 'a Stdlib.List.t = | []| :: of 'a * 'a list
in-monad, preallocated nil
val nil_e : ('a list, 'trace) Stdlib.resultval nil_s : 'a list Lwt.tval nil_es : ('a list, 'trace) Stdlib.result Lwt.tnil is Lwt.return (Ok [])
Safe wrappers
Shadowing unsafe functions to avoid all exceptions.
Safe lookups, scans, retrievals
Return option rather than raise Not_found, Failure _, or Invalid_argument _
val hd : 'a list -> 'a optionhd xs is the head (first element) of the list or None if the list is empty.
val tl : 'a list -> 'a list optiontl xs is the tail of the list (the whole list except the first element) or None if the list is empty.
val nth : 'a list -> int -> 'a optionnth xs n is the nth element of the list or None if the list has fewer than n elements.
nth xs 0 = tl xs
val nth_opt : 'a list -> int -> 'a optionnth_opt is an alias for nth provided for backwards compatibility.
val last : 'a -> 'a list -> 'alast x xs is the last element of the list xs or x if xs is empty.
The primary intended use for last is after destructing a list: match l with | None -> … | Some x :: xs -> last x xs but it can also be used for a default value: last default_value_if_empty xs.
val last_opt : 'a list -> 'a optionlast_opt xs is the last element of the list xs or None if the list xs is empty.
val find : ('a -> bool) -> 'a list -> 'a optionfind predicate xs is the first element x of the list xs such that predicate x is true or None if the list xs has no such element.
val find_opt : ('a -> bool) -> 'a list -> 'a optionfind_opt is an alias for find provided for backwards compatibility.
val mem : equal:('a -> 'a -> bool) -> 'a -> 'a list -> boolmem ~equal a l is true iff there is an element e of l such that equal a e.
val assoc : equal:('a -> 'a -> bool) -> 'a -> ('a * 'b) list -> 'b optionassoc ~equal k kvs is Some v such that (k', v) is the first pair in the list such that equal k' k or None if the list contains no such pair.
val assoc_opt : equal:('a -> 'a -> bool) -> 'a -> ('a * 'b) list -> 'b optionassoc_opt is an alias for assoc provided for backwards compatibility.
val assq : 'a -> ('a * 'b) list -> 'b optionassq k kvs is the same as assoc ~equal:Stdlib.( == ) k kvs: it uses the physical equality.
val assq_opt : 'a -> ('a * 'b) list -> 'b optionassq_opt is an alias for assq provided for backwards compatibility.
val mem_assoc : equal:('a -> 'a -> bool) -> 'a -> ('a * 'b) list -> boolmem_assoc ~equal k l is equivalent to Option.is_some @@ assoc ~equal k l.
val mem_assq : 'a -> ('a * 'b) list -> boolmem_assq k l is mem_assoc ~equal:Stdlib.( == ) k l.
val remove_assoc :
equal:('a -> 'a -> bool) ->
'a ->
('a * 'b) list ->
('a * 'b) listremove_assoc ~equal k l is l without the first element (k', _) such that equal k k'.
val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) listremove_assoq k l is remove_assoc ~equal:Stdlib.( == ) k l.
Initialisation
val init :
when_negative_length:'trace ->
int ->
(int -> 'a) ->
('a list, 'trace) Stdlib.resultinit ~when_negative_length n f is Error when_negative_length if n is strictly negative and Ok (Stdlib.List.init n f) otherwise.
Basic traversal
val length : 'a list -> intval rev : 'a list -> 'a listval concat : 'a list list -> 'a listval append : 'a list -> 'a list -> 'a listval rev_append : 'a list -> 'a list -> 'a listval flatten : 'a list list -> 'a listDouble-list traversals
These safe-wrappers take an explicit value to handle the case of lists of unequal length.
val combine :
when_different_lengths:'trace ->
'a list ->
'b list ->
(('a * 'b) list, 'trace) Stdlib.resultcombine ~when_different_lengths l1 l2 is either
Error when_different_lengths if List.length l1 <> List.length l2- a list of pairs of elements from
l1 and l2
E.g., combine ~when_different_lengths [] [] = Ok []
E.g., combine ~when_different_lengths [1; 2] ['a'; 'b'] = Ok [(1,'a'); (2, 'b')]
E.g., combine ~when_different_lengths:() [1] [] = Error ()
Note: combine ~when_different_lengths l1 l2 is equivalent to try Ok (Stdlib.List.combine l1 l2) with Invalid_argument _ -> when_different_lengths
The same equivalence almost holds for the other double traversors below. The notable difference is if the functions passed as argument to the traversors raise the Invalid_argument _ exception.
val rev_combine :
when_different_lengths:'trace ->
'a list ->
'b list ->
(('a * 'b) list, 'trace) Stdlib.resultrev_combine ~when_different_lengths xs ys is rev (combine ~when_different_lengths xs ys) but more efficient.
val split : ('a * 'b) list -> 'a list * 'b listval iter2 :
when_different_lengths:'trace ->
('a -> 'b -> unit) ->
'a list ->
'b list ->
(unit, 'trace) Stdlib.resultval map2 :
when_different_lengths:'trace ->
('a -> 'b -> 'c) ->
'a list ->
'b list ->
('c list, 'trace) Stdlib.resultval rev_map2 :
when_different_lengths:'trace ->
('a -> 'b -> 'c) ->
'a list ->
'b list ->
('c list, 'trace) Stdlib.resultval fold_left2 :
when_different_lengths:'trace ->
('a -> 'b -> 'c -> 'a) ->
'a ->
'b list ->
'c list ->
('a, 'trace) Stdlib.resultval fold_right2 :
when_different_lengths:'trace ->
('a -> 'b -> 'c -> 'c) ->
'a list ->
'b list ->
'c ->
('c, 'trace) Stdlib.resultThis function is not tail-recursive
val for_all2 :
when_different_lengths:'trace ->
('a -> 'b -> bool) ->
'a list ->
'b list ->
(bool, 'trace) Stdlib.resultval exists2 :
when_different_lengths:'trace ->
('a -> 'b -> bool) ->
'a list ->
'b list ->
(bool, 'trace) Stdlib.resultMonad-aware variants
The functions below are strict extensions of the standard Stdlib.List module. It is for result-, lwt- and lwt-result-aware variants. The meaning of the suffix is as described above, in Lwtreslib, and in Sigs.Seq.
Initialisation variants
Note that for asynchronous variants (_s, _es, _p, and _ep), if the length parameter is negative, then the promise is returned already fulfilled with Error when_different_lengths.
val init_e :
when_negative_length:'trace ->
int ->
(int -> ('a, 'trace) Stdlib.result) ->
('a list, 'trace) Stdlib.resultval init_s :
when_negative_length:'trace ->
int ->
(int -> 'a Lwt.t) ->
('a list, 'trace) Stdlib.result Lwt.tval init_es :
when_negative_length:'trace ->
int ->
(int -> ('a, 'trace) Stdlib.result Lwt.t) ->
('a list, 'trace) Stdlib.result Lwt.tval init_p :
when_negative_length:'trace ->
int ->
(int -> 'a Lwt.t) ->
('a list, 'trace) Stdlib.result Lwt.tQuery variants
val find_e :
('a -> (bool, 'trace) Stdlib.result) ->
'a list ->
('a option, 'trace) Stdlib.resultval find_s : ('a -> bool Lwt.t) -> 'a list -> 'a option Lwt.tval find_es :
('a -> (bool, 'trace) Stdlib.result Lwt.t) ->
'a list ->
('a option, 'trace) Stdlib.result Lwt.tval filter : ('a -> bool) -> 'a list -> 'a listval rev_filter : ('a -> bool) -> 'a list -> 'a listrev_filter f l is rev (filter f l) but more efficient.
val rev_filter_some : 'a option list -> 'a listval filter_some : 'a option list -> 'a listval rev_filter_ok : ('a, 'b) Stdlib.result list -> 'a listval filter_ok : ('a, 'b) Stdlib.result list -> 'a listval rev_filter_error : ('a, 'b) Stdlib.result list -> 'b listval filter_error : ('a, 'b) Stdlib.result list -> 'b listval rev_filter_e :
('a -> (bool, 'trace) Stdlib.result) ->
'a list ->
('a list, 'trace) Stdlib.resultval filter_e :
('a -> (bool, 'trace) Stdlib.result) ->
'a list ->
('a list, 'trace) Stdlib.resultval rev_filter_s : ('a -> bool Lwt.t) -> 'a list -> 'a list Lwt.tval filter_s : ('a -> bool Lwt.t) -> 'a list -> 'a list Lwt.tval rev_filter_es :
('a -> (bool, 'trace) Stdlib.result Lwt.t) ->
'a list ->
('a list, 'trace) Stdlib.result Lwt.tval filter_es :
('a -> (bool, 'trace) Stdlib.result Lwt.t) ->
'a list ->
('a list, 'trace) Stdlib.result Lwt.tval filter_p : ('a -> bool Lwt.t) -> 'a list -> 'a list Lwt.tval rev_partition : ('a -> bool) -> 'a list -> 'a list * 'a listval partition : ('a -> bool) -> 'a list -> 'a list * 'a listval rev_partition_result : ('a, 'b) Stdlib.result list -> 'a list * 'b listval partition_result : ('a, 'b) Stdlib.result list -> 'a list * 'b listval rev_partition_e :
('a -> (bool, 'trace) Stdlib.result) ->
'a list ->
('a list * 'a list, 'trace) Stdlib.resultval partition_e :
('a -> (bool, 'trace) Stdlib.result) ->
'a list ->
('a list * 'a list, 'trace) Stdlib.resultval rev_partition_s :
('a -> bool Lwt.t) ->
'a list ->
('a list * 'a list) Lwt.tval partition_s : ('a -> bool Lwt.t) -> 'a list -> ('a list * 'a list) Lwt.tval rev_partition_es :
('a -> (bool, 'trace) Stdlib.result Lwt.t) ->
'a list ->
('a list * 'a list, 'trace) Stdlib.result Lwt.tval partition_es :
('a -> (bool, 'trace) Stdlib.result Lwt.t) ->
'a list ->
('a list * 'a list, 'trace) Stdlib.result Lwt.tval partition_p : ('a -> bool Lwt.t) -> 'a list -> ('a list * 'a list) Lwt.tval iter : ('a -> unit) -> 'a list -> unitval iter_e :
('a -> (unit, 'trace) Stdlib.result) ->
'a list ->
(unit, 'trace) Stdlib.resultval iter_s : ('a -> unit Lwt.t) -> 'a list -> unit Lwt.tval iter_es :
('a -> (unit, 'trace) Stdlib.result Lwt.t) ->
'a list ->
(unit, 'trace) Stdlib.result Lwt.tval iter_p : ('a -> unit Lwt.t) -> 'a list -> unit Lwt.tval iteri : (int -> 'a -> unit) -> 'a list -> unitval iteri_e :
(int -> 'a -> (unit, 'trace) Stdlib.result) ->
'a list ->
(unit, 'trace) Stdlib.resultval iteri_s : (int -> 'a -> unit Lwt.t) -> 'a list -> unit Lwt.tval iteri_es :
(int -> 'a -> (unit, 'trace) Stdlib.result Lwt.t) ->
'a list ->
(unit, 'trace) Stdlib.result Lwt.tval iteri_p : (int -> 'a -> unit Lwt.t) -> 'a list -> unit Lwt.tval map : ('a -> 'b) -> 'a list -> 'b listval map_e :
('a -> ('b, 'trace) Stdlib.result) ->
'a list ->
('b list, 'trace) Stdlib.resultval map_s : ('a -> 'b Lwt.t) -> 'a list -> 'b list Lwt.tval map_es :
('a -> ('b, 'trace) Stdlib.result Lwt.t) ->
'a list ->
('b list, 'trace) Stdlib.result Lwt.tval map_p : ('a -> 'b Lwt.t) -> 'a list -> 'b list Lwt.tval mapi : (int -> 'a -> 'b) -> 'a list -> 'b listval mapi_e :
(int -> 'a -> ('b, 'trace) Stdlib.result) ->
'a list ->
('b list, 'trace) Stdlib.resultval mapi_s : (int -> 'a -> 'b Lwt.t) -> 'a list -> 'b list Lwt.tval mapi_es :
(int -> 'a -> ('b, 'trace) Stdlib.result Lwt.t) ->
'a list ->
('b list, 'trace) Stdlib.result Lwt.tval mapi_p : (int -> 'a -> 'b Lwt.t) -> 'a list -> 'b list Lwt.tval rev_map : ('a -> 'b) -> 'a list -> 'b listval rev_mapi : (int -> 'a -> 'b) -> 'a list -> 'b listval rev_map_e :
('a -> ('b, 'trace) Stdlib.result) ->
'a list ->
('b list, 'trace) Stdlib.resultval rev_map_s : ('a -> 'b Lwt.t) -> 'a list -> 'b list Lwt.tval rev_map_es :
('a -> ('b, 'trace) Stdlib.result Lwt.t) ->
'a list ->
('b list, 'trace) Stdlib.result Lwt.tval rev_map_p : ('a -> 'b Lwt.t) -> 'a list -> 'b list Lwt.tval rev_mapi_e :
(int -> 'a -> ('b, 'trace) Stdlib.result) ->
'a list ->
('b list, 'trace) Stdlib.resultval rev_mapi_s : (int -> 'a -> 'b Lwt.t) -> 'a list -> 'b list Lwt.tval rev_mapi_es :
(int -> 'a -> ('b, 'trace) Stdlib.result Lwt.t) ->
'a list ->
('b list, 'trace) Stdlib.result Lwt.tval rev_mapi_p : (int -> 'a -> 'b Lwt.t) -> 'a list -> 'b list Lwt.tval rev_filter_map : ('a -> 'b option) -> 'a list -> 'b listval rev_filter_map_e :
('a -> ('b option, 'trace) Stdlib.result) ->
'a list ->
('b list, 'trace) Stdlib.resultval filter_map_e :
('a -> ('b option, 'trace) Stdlib.result) ->
'a list ->
('b list, 'trace) Stdlib.resultval rev_filter_map_s : ('a -> 'b option Lwt.t) -> 'a list -> 'b list Lwt.tval filter_map : ('a -> 'b option) -> 'a list -> 'b listval filter_map_s : ('a -> 'b option Lwt.t) -> 'a list -> 'b list Lwt.tval rev_filter_map_es :
('a -> ('b option, 'trace) Stdlib.result Lwt.t) ->
'a list ->
('b list, 'trace) Stdlib.result Lwt.tval filter_map_es :
('a -> ('b option, 'trace) Stdlib.result Lwt.t) ->
'a list ->
('b list, 'trace) Stdlib.result Lwt.tval filter_map_p : ('a -> 'b option Lwt.t) -> 'a list -> 'b list Lwt.tval fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'aval fold_left_e :
('a -> 'b -> ('a, 'trace) Stdlib.result) ->
'a ->
'b list ->
('a, 'trace) Stdlib.resultval fold_left_s : ('a -> 'b -> 'a Lwt.t) -> 'a -> 'b list -> 'a Lwt.tval fold_left_es :
('a -> 'b -> ('a, 'trace) Stdlib.result Lwt.t) ->
'a ->
'b list ->
('a, 'trace) Stdlib.result Lwt.tval fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'bThis function is not tail-recursive
val fold_right_e :
('a -> 'b -> ('b, 'trace) Stdlib.result) ->
'a list ->
'b ->
('b, 'trace) Stdlib.resultThis function is not tail-recursive
val fold_right_s : ('a -> 'b -> 'b Lwt.t) -> 'a list -> 'b -> 'b Lwt.tThis function is not tail-recursive
val fold_right_es :
('a -> 'b -> ('b, 'trace) Stdlib.result Lwt.t) ->
'a list ->
'b ->
('b, 'trace) Stdlib.result Lwt.tThis function is not tail-recursive
Double-traversal variants
As mentioned above, there are no _p and _ep double-traversors. Use combine (and variants) to circumvent this.
val iter2_e :
when_different_lengths:'trace ->
('a -> 'b -> (unit, 'trace) Stdlib.result) ->
'a list ->
'b list ->
(unit, 'trace) Stdlib.resultval iter2_s :
when_different_lengths:'trace ->
('a -> 'b -> unit Lwt.t) ->
'a list ->
'b list ->
(unit, 'trace) Stdlib.result Lwt.tval iter2_es :
when_different_lengths:'trace ->
('a -> 'b -> (unit, 'trace) Stdlib.result Lwt.t) ->
'a list ->
'b list ->
(unit, 'trace) Stdlib.result Lwt.tval map2_e :
when_different_lengths:'trace ->
('a -> 'b -> ('c, 'trace) Stdlib.result) ->
'a list ->
'b list ->
('c list, 'trace) Stdlib.resultval map2_s :
when_different_lengths:'trace ->
('a -> 'b -> 'c Lwt.t) ->
'a list ->
'b list ->
('c list, 'trace) Stdlib.result Lwt.tval map2_es :
when_different_lengths:'trace ->
('a -> 'b -> ('c, 'trace) Stdlib.result Lwt.t) ->
'a list ->
'b list ->
('c list, 'trace) Stdlib.result Lwt.tval rev_map2_e :
when_different_lengths:'trace ->
('a -> 'b -> ('c, 'trace) Stdlib.result) ->
'a list ->
'b list ->
('c list, 'trace) Stdlib.resultval rev_map2_s :
when_different_lengths:'trace ->
('a -> 'b -> 'c Lwt.t) ->
'a list ->
'b list ->
('c list, 'trace) Stdlib.result Lwt.tval rev_map2_es :
when_different_lengths:'trace ->
('a -> 'b -> ('c, 'trace) Stdlib.result Lwt.t) ->
'a list ->
'b list ->
('c list, 'trace) Stdlib.result Lwt.tval fold_left2_e :
when_different_lengths:'trace ->
('a -> 'b -> 'c -> ('a, 'trace) Stdlib.result) ->
'a ->
'b list ->
'c list ->
('a, 'trace) Stdlib.resultval fold_left2_s :
when_different_lengths:'trace ->
('a -> 'b -> 'c -> 'a Lwt.t) ->
'a ->
'b list ->
'c list ->
('a, 'trace) Stdlib.result Lwt.tval fold_left2_es :
when_different_lengths:'trace ->
('a -> 'b -> 'c -> ('a, 'trace) Stdlib.result Lwt.t) ->
'a ->
'b list ->
'c list ->
('a, 'trace) Stdlib.result Lwt.tval fold_right2_e :
when_different_lengths:'trace ->
('a -> 'b -> 'c -> ('c, 'trace) Stdlib.result) ->
'a list ->
'b list ->
'c ->
('c, 'trace) Stdlib.resultThis function is not tail-recursive
val fold_right2_s :
when_different_lengths:'trace ->
('a -> 'b -> 'c -> 'c Lwt.t) ->
'a list ->
'b list ->
'c ->
('c, 'trace) Stdlib.result Lwt.tThis function is not tail-recursive
val fold_right2_es :
when_different_lengths:'trace ->
('a -> 'b -> 'c -> ('c, 'trace) Stdlib.result Lwt.t) ->
'a list ->
'b list ->
'c ->
('c, 'trace) Stdlib.result Lwt.tThis function is not tail-recursive
Scanning variants
val for_all : ('a -> bool) -> 'a list -> boolval for_all_e :
('a -> (bool, 'trace) Stdlib.result) ->
'a list ->
(bool, 'trace) Stdlib.resultval for_all_s : ('a -> bool Lwt.t) -> 'a list -> bool Lwt.tval for_all_es :
('a -> (bool, 'trace) Stdlib.result Lwt.t) ->
'a list ->
(bool, 'trace) Stdlib.result Lwt.tval for_all_p : ('a -> bool Lwt.t) -> 'a list -> bool Lwt.tval exists : ('a -> bool) -> 'a list -> boolval exists_e :
('a -> (bool, 'trace) Stdlib.result) ->
'a list ->
(bool, 'trace) Stdlib.resultval exists_s : ('a -> bool Lwt.t) -> 'a list -> bool Lwt.tval exists_es :
('a -> (bool, 'trace) Stdlib.result Lwt.t) ->
'a list ->
(bool, 'trace) Stdlib.result Lwt.tval exists_p : ('a -> bool Lwt.t) -> 'a list -> bool Lwt.tDouble-scanning variants
As mentioned above, there are no _p and _ep double-scanners. Use combine (and variants) to circumvent this.
val for_all2_e :
when_different_lengths:'trace ->
('a -> 'b -> (bool, 'trace) Stdlib.result) ->
'a list ->
'b list ->
(bool, 'trace) Stdlib.resultval for_all2_s :
when_different_lengths:'trace ->
('a -> 'b -> bool Lwt.t) ->
'a list ->
'b list ->
(bool, 'trace) Stdlib.result Lwt.tval for_all2_es :
when_different_lengths:'trace ->
('a -> 'b -> (bool, 'trace) Stdlib.result Lwt.t) ->
'a list ->
'b list ->
(bool, 'trace) Stdlib.result Lwt.tval exists2_e :
when_different_lengths:'trace ->
('a -> 'b -> (bool, 'trace) Stdlib.result) ->
'a list ->
'b list ->
(bool, 'trace) Stdlib.resultval exists2_s :
when_different_lengths:'trace ->
('a -> 'b -> bool Lwt.t) ->
'a list ->
'b list ->
(bool, 'trace) Stdlib.result Lwt.tval exists2_es :
when_different_lengths:'trace ->
('a -> 'b -> (bool, 'trace) Stdlib.result Lwt.t) ->
'a list ->
'b list ->
(bool, 'trace) Stdlib.result Lwt.tCombine variants
These are primarily intended to be used for preprocessing before applying a traversor to the resulting list of pairs. They give alternatives to the when_different_lengths mechanism of the immediate double-traversors above.
In case the semantic of, say, map2_es was unsatisfying, one can use map_es on a combine-preprocessed pair of lists. The different variants of combine give different approaches to different-length handling.
val combine_drop : 'a list -> 'b list -> ('a * 'b) listcombine_drop ll lr is a list l of pairs of elements taken from the common-length prefix of ll and lr. The suffix of whichever list is longer (if any) is dropped.
More formally nth l n is:
None if n >= min (length ll) (length lr)Some (Option.get @@ nth ll n, Option.get @@ nth lr n) otherwise
val combine_with_leftovers :
'a list ->
'b list ->
('a * 'b) list * [ `Left of 'a list | `Right of 'b list ] optioncombine_with_leftovers ll lr is a tuple (combined, leftover) where combined is combine_drop ll lr and leftover is either `Left lsuffix or `Right rsuffix depending on which of ll or lr is longer. leftover is None if the two lists have the same length.
compare / equal
val compare : ('a -> 'a -> int) -> 'a list -> 'a list -> intval equal : ('a -> 'a -> bool) -> 'a list -> 'a list -> boolSorting
val sort : ('a -> 'a -> int) -> 'a list -> 'a listval stable_sort : ('a -> 'a -> int) -> 'a list -> 'a listval fast_sort : ('a -> 'a -> int) -> 'a list -> 'a listval sort_uniq : ('a -> 'a -> int) -> 'a list -> 'a listconversion
val to_seq : 'a t -> 'a Stdlib.Seq.tval of_seq : 'a Stdlib.Seq.t -> 'a list