diff --git a/_tags b/_tags index 89d3e6b6..08c6f6f5 100644 --- a/_tags +++ b/_tags @@ -157,7 +157,7 @@ true: annot, bin_annot : thread or : inline(25) or or : inline(15) - or : warn(-32) + or or : warn(-32) and not : warn(+a-4-44-58-60@8) true: no_alias_deps, safe_string, short_paths : nolabels diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 89dd6a80..f7552587 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -9,6 +9,53 @@ type 'a t = 'a list +(* backport new functions from stdlib here *) + +let nth_opt l n = + if n<0 then invalid_arg "nth_opt"; + let rec aux l n = match l, n with + | [], _ -> None + | x::_, 0 -> Some x + | _::l, _ -> aux l (n-1) + in + aux l n + +(*$Q + Q.(pair small_nat (list int)) (fun (i,l) -> \ + nth_opt l i = get_at_idx i l) +*) + +let rec find_opt p l = match l with + | [] -> None + | x :: _ when p x -> Some x + | _ :: tl -> find_opt p tl + +let rec compare_lengths l1 l2 = match l1, l2 with + | [], [] -> 0 + | [], _::_ -> 1 + | _::_, [] -> -1 + | _::tail1, _::tail2 -> compare_lengths tail1 tail2 + +(*$Q + Q.(pair (list int) (list int)) (fun (l1,l2) -> \ + CCOrd.equiv (compare_lengths l1 l2) \ + (Pervasives.compare (length l1)(length l2))) +*) + +let rec compare_length_with l n = match l, n with + | _ when n<0 -> -1 + | [], 0 -> 0 + | [], _ -> 1 + | _::tail, _ -> compare_length_with tail (n-1) + +(*$Q + Q.(pair (list int) small_nat) (fun (l,n) -> \ + CCOrd.equiv (compare_length_with l n) \ + (Pervasives.compare (length l)n)) +*) + +(* end of backport *) + include List let empty = [] @@ -802,10 +849,7 @@ let rec last_opt = function None (last_opt []) *) -let rec find_pred p l = match l with - | [] -> None - | x :: _ when p x -> Some x - | _ :: tl -> find_pred p tl +let find_pred = find_opt let find_pred_exn p l = match find_pred p l with | None -> raise Not_found diff --git a/src/core/CCList.mli b/src/core/CCList.mli index bd692957..f83407bf 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -97,6 +97,14 @@ val split : ('a * 'b) t -> 'a t * 'b t val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int +val compare_lengths : 'a t -> 'b t -> int +(** equivalent to [compare (length l1) (length l2)] but more efficient. + @since NEXT_RELEASE *) + +val compare_length_with : 'a t -> int -> int +(** equivalent to [compare (length l) x] but more efficient. + @since NEXT_RELEASE *) + val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool val flat_map : ('a -> 'b t) -> 'a t -> 'b t @@ -225,6 +233,10 @@ val find_pred : ('a -> bool) -> 'a t -> 'a option or returns [None] if no element satisfies [p] @since 0.11 *) +val find_opt : ('a -> bool) -> 'a t -> 'a option +(** Safe version of {!find} + @since NEXT_RELEASE *) + val find_pred_exn : ('a -> bool) -> 'a t -> 'a (** Unsafe version of {!find_pred} @raise Not_found if no such element is found @@ -324,6 +336,11 @@ val foldi : ('b -> int -> 'a -> 'b) -> 'b -> 'a t -> 'b val get_at_idx : int -> 'a t -> 'a option +val nth_opt : 'a t -> int -> 'a option +(** Safe version of {!nth}. + @raise Invalid_argument if the int is negative. + @since NEXT_RELEASE *) + val get_at_idx_exn : int -> 'a t -> 'a (** Get the i-th element, or @raise Not_found if the index is invalid *) @@ -432,6 +449,14 @@ module Assoc : sig @since 0.17 *) end +val assoc_opt : 'a -> ('a * 'b) t -> 'b option +(** Safe version of {!assoc} + @since NEXT_RELEASE *) + +val assq_opt : 'a -> ('a * 'b) t -> 'b option +(** Safe version of {!assq} + @since NEXT_RELEASE *) + (** {2 References on Lists} @since 0.3.3 *)