mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
add CCList.Assoc.mem
This commit is contained in:
parent
12fe3fdde3
commit
dcfbff7827
2 changed files with 21 additions and 8 deletions
|
|
@ -793,15 +793,15 @@ let repeat i l =
|
|||
module Assoc = struct
|
||||
type ('a, 'b) t = ('a*'b) list
|
||||
|
||||
let get_exn ?(eq=(=)) l x =
|
||||
let rec search eq l x = match l with
|
||||
| [] -> raise Not_found
|
||||
| (y,z)::l' ->
|
||||
if eq x y then z else search eq l' x
|
||||
in search eq l x
|
||||
let rec search_exn eq l x = match l with
|
||||
| [] -> raise Not_found
|
||||
| (y,z)::l' ->
|
||||
if eq x y then z else search_exn eq l' x
|
||||
|
||||
let get ?eq l x =
|
||||
try Some (get_exn ?eq l x)
|
||||
let get_exn ?(eq=(=)) l x = search_exn eq l x
|
||||
|
||||
let get ?(eq=(=)) l x =
|
||||
try Some (search_exn eq l x)
|
||||
with Not_found -> None
|
||||
|
||||
(*$T
|
||||
|
|
@ -826,6 +826,15 @@ module Assoc = struct
|
|||
Assoc.set [1,"1"; 2, "2"] 3 "3" |> List.sort Pervasives.compare \
|
||||
= [1, "1"; 2, "2"; 3, "3"]
|
||||
*)
|
||||
|
||||
let mem ?(eq=(=)) l x =
|
||||
try ignore (search_exn eq l x); true
|
||||
with Not_found -> false
|
||||
|
||||
(*$T
|
||||
Assoc.mem [1,"1"; 2,"2"; 3, "3"] 1
|
||||
not (Assoc.mem [1,"1"; 2,"2"; 3, "3"] 4)
|
||||
*)
|
||||
end
|
||||
|
||||
(** {2 Zipper} *)
|
||||
|
|
|
|||
|
|
@ -300,6 +300,10 @@ module Assoc : sig
|
|||
|
||||
val set : ?eq:('a->'a->bool) -> ('a,'b) t -> 'a -> 'b -> ('a,'b) t
|
||||
(** Add the binding into the list (erase it if already present) *)
|
||||
|
||||
val mem : ?eq:('a->'a->bool) -> ('a,_) t -> 'a -> bool
|
||||
(** [mem l x] returns [true] iff [x] is a key in [l]
|
||||
@since NEXT_RELEASE *)
|
||||
end
|
||||
|
||||
(** {2 Zipper} *)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue