mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
backport some functions added in 4.05 in CCList
This commit is contained in:
parent
f65bcd212d
commit
946a585a9e
3 changed files with 74 additions and 5 deletions
2
_tags
2
_tags
|
|
@ -157,7 +157,7 @@ true: annot, bin_annot
|
|||
<src/threads/*.ml{,i}>: thread
|
||||
<src/core/CCVector.cmx> or <src/core/CCString.cmx>: inline(25)
|
||||
<src/data/CCFlatHashtbl.cm*> or <src/data/CCHashTrie.cm*> or <src/data/CCPersistent*>: inline(15)
|
||||
<src/core/CCMap.*> or <src/core/CCSet.*>: warn(-32)
|
||||
<src/core/CCMap.*> or <src/core/CCSet.*> or <src/core/CCList.*>: warn(-32)
|
||||
<src/**/*.ml> and not <src/misc/*.ml>: warn(+a-4-44-58-60@8)
|
||||
true: no_alias_deps, safe_string, short_paths
|
||||
<src/**/*Labels.cm*>: nolabels
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue