mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
replace or_ by default in labelled functions
This commit is contained in:
parent
601354305d
commit
ed38c25711
4 changed files with 28 additions and 28 deletions
|
|
@ -14,13 +14,13 @@ let get tbl x =
|
||||||
try Some (Hashtbl.find tbl x)
|
try Some (Hashtbl.find tbl x)
|
||||||
with Not_found -> None
|
with Not_found -> None
|
||||||
|
|
||||||
let get_or tbl x ~or_ =
|
let get_or tbl x ~default =
|
||||||
try Hashtbl.find tbl x
|
try Hashtbl.find tbl x
|
||||||
with Not_found -> or_
|
with Not_found -> default
|
||||||
|
|
||||||
(*$=
|
(*$=
|
||||||
"c" (let tbl = of_list [1,"a"; 2,"b"] in get_or tbl 3 ~or_:"c")
|
"c" (let tbl = of_list [1,"a"; 2,"b"] in get_or tbl 3 ~default:"c")
|
||||||
"b" (let tbl = of_list [1,"a"; 2,"b"] in get_or tbl 2 ~or_:"c")
|
"b" (let tbl = of_list [1,"a"; 2,"b"] in get_or tbl 2 ~default:"c")
|
||||||
*)
|
*)
|
||||||
|
|
||||||
let keys tbl k = Hashtbl.iter (fun key _ -> k key) tbl
|
let keys tbl k = Hashtbl.iter (fun key _ -> k key) tbl
|
||||||
|
|
@ -35,7 +35,7 @@ let add_list tbl k v =
|
||||||
Hashtbl.replace tbl k (v::l)
|
Hashtbl.replace tbl k (v::l)
|
||||||
|
|
||||||
let incr ?(by=1) tbl x =
|
let incr ?(by=1) tbl x =
|
||||||
let n = get_or tbl x ~or_:0 in
|
let n = get_or tbl x ~default:0 in
|
||||||
if n+by <= 0
|
if n+by <= 0
|
||||||
then Hashtbl.remove tbl x
|
then Hashtbl.remove tbl x
|
||||||
else Hashtbl.replace tbl x (n+by)
|
else Hashtbl.replace tbl x (n+by)
|
||||||
|
|
@ -124,9 +124,9 @@ module type S = sig
|
||||||
val get : 'a t -> key -> 'a option
|
val get : 'a t -> key -> 'a option
|
||||||
(** Safe version of {!Hashtbl.find} *)
|
(** Safe version of {!Hashtbl.find} *)
|
||||||
|
|
||||||
val get_or : 'a t -> key -> or_:'a -> 'a
|
val get_or : 'a t -> key -> default:'a -> 'a
|
||||||
(** [get_or tbl k ~or_] returns the value associated to [k] if present,
|
(** [get_or tbl k ~default] returns the value associated to [k] if present,
|
||||||
and returns [or_] otherwise (if [k] doesn't belong in [tbl])
|
and returns [default] otherwise (if [k] doesn't belong in [tbl])
|
||||||
@since 0.16 *)
|
@since 0.16 *)
|
||||||
|
|
||||||
val add_list : 'a list t -> key -> 'a -> unit
|
val add_list : 'a list t -> key -> 'a -> unit
|
||||||
|
|
@ -216,17 +216,17 @@ module Make(X : Hashtbl.HashedType)
|
||||||
try Some (find tbl x)
|
try Some (find tbl x)
|
||||||
with Not_found -> None
|
with Not_found -> None
|
||||||
|
|
||||||
let get_or tbl x ~or_ =
|
let get_or tbl x ~default =
|
||||||
try find tbl x
|
try find tbl x
|
||||||
with Not_found -> or_
|
with Not_found -> default
|
||||||
|
|
||||||
(*$=
|
(*$=
|
||||||
"c" (let tbl = T.of_list [1,"a"; 2,"b"] in T.get_or tbl 3 ~or_:"c")
|
"c" (let tbl = T.of_list [1,"a"; 2,"b"] in T.get_or tbl 3 ~default:"c")
|
||||||
"b" (let tbl = T.of_list [1,"a"; 2,"b"] in T.get_or tbl 2 ~or_:"c")
|
"b" (let tbl = T.of_list [1,"a"; 2,"b"] in T.get_or tbl 2 ~default:"c")
|
||||||
*)
|
*)
|
||||||
|
|
||||||
let incr ?(by=1) tbl x =
|
let incr ?(by=1) tbl x =
|
||||||
let n = get_or tbl x ~or_:0 in
|
let n = get_or tbl x ~default:0 in
|
||||||
if n+by <= 0
|
if n+by <= 0
|
||||||
then remove tbl x
|
then remove tbl x
|
||||||
else replace tbl x (n+by)
|
else replace tbl x (n+by)
|
||||||
|
|
@ -240,7 +240,7 @@ module Make(X : Hashtbl.HashedType)
|
||||||
assert_equal 1 (T.find tbl 2);
|
assert_equal 1 (T.find tbl 2);
|
||||||
assert_equal 2 (T.length tbl);
|
assert_equal 2 (T.length tbl);
|
||||||
T.decr tbl 2;
|
T.decr tbl 2;
|
||||||
assert_equal 0 (T.get_or tbl 2 ~or_:0);
|
assert_equal 0 (T.get_or tbl 2 ~default:0);
|
||||||
assert_equal 1 (T.length tbl);
|
assert_equal 1 (T.length tbl);
|
||||||
assert_bool "2 removed" (not (T.mem tbl 2));
|
assert_bool "2 removed" (not (T.mem tbl 2));
|
||||||
*)
|
*)
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,9 @@ type 'a printer = Format.formatter -> 'a -> unit
|
||||||
val get : ('a,'b) Hashtbl.t -> 'a -> 'b option
|
val get : ('a,'b) Hashtbl.t -> 'a -> 'b option
|
||||||
(** Safe version of {!Hashtbl.find} *)
|
(** Safe version of {!Hashtbl.find} *)
|
||||||
|
|
||||||
val get_or : ('a,'b) Hashtbl.t -> 'a -> or_:'b -> 'b
|
val get_or : ('a,'b) Hashtbl.t -> 'a -> default:'b -> 'b
|
||||||
(** [get_or tbl k ~or_] returns the value associated to [k] if present,
|
(** [get_or tbl k ~default] returns the value associated to [k] if present,
|
||||||
and returns [or_] otherwise (if [k] doesn't belong in [tbl])
|
and returns [default] otherwise (if [k] doesn't belong in [tbl])
|
||||||
@since 0.16 *)
|
@since 0.16 *)
|
||||||
|
|
||||||
val keys : ('a,'b) Hashtbl.t -> 'a sequence
|
val keys : ('a,'b) Hashtbl.t -> 'a sequence
|
||||||
|
|
@ -104,9 +104,9 @@ module type S = sig
|
||||||
val get : 'a t -> key -> 'a option
|
val get : 'a t -> key -> 'a option
|
||||||
(** Safe version of {!Hashtbl.find} *)
|
(** Safe version of {!Hashtbl.find} *)
|
||||||
|
|
||||||
val get_or : 'a t -> key -> or_:'a -> 'a
|
val get_or : 'a t -> key -> default:'a -> 'a
|
||||||
(** [get_or tbl k ~or_] returns the value associated to [k] if present,
|
(** [get_or tbl k ~default] returns the value associated to [k] if present,
|
||||||
and returns [or_] otherwise (if [k] doesn't belong in [tbl])
|
and returns [default] otherwise (if [k] doesn't belong in [tbl])
|
||||||
@since 0.16 *)
|
@since 0.16 *)
|
||||||
|
|
||||||
val add_list : 'a list t -> key -> 'a -> unit
|
val add_list : 'a list t -> key -> 'a -> unit
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,9 @@ module type S = sig
|
||||||
val get : key -> 'a t -> 'a option
|
val get : key -> 'a t -> 'a option
|
||||||
(** Safe version of {!find} *)
|
(** Safe version of {!find} *)
|
||||||
|
|
||||||
val get_or : key -> 'a t -> or_:'a -> 'a
|
val get_or : key -> 'a t -> default:'a -> 'a
|
||||||
(** [get_or k m ~or_] returns the value associated to [k] if present,
|
(** [get_or k m ~default] returns the value associated to [k] if present,
|
||||||
and returns [or_] otherwise (if [k] doesn't belong in [m])
|
and returns [default] otherwise (if [k] doesn't belong in [m])
|
||||||
@since 0.16 *)
|
@since 0.16 *)
|
||||||
|
|
||||||
val update : key -> ('a option -> 'a option) -> 'a t -> 'a t
|
val update : key -> ('a option -> 'a option) -> 'a t -> 'a t
|
||||||
|
|
@ -63,9 +63,9 @@ module Make(O : Map.OrderedType) = struct
|
||||||
try Some (find k m)
|
try Some (find k m)
|
||||||
with Not_found -> None
|
with Not_found -> None
|
||||||
|
|
||||||
let get_or k m ~or_ =
|
let get_or k m ~default =
|
||||||
try find k m
|
try find k m
|
||||||
with Not_found -> or_
|
with Not_found -> default
|
||||||
|
|
||||||
let update k f m =
|
let update k f m =
|
||||||
let x =
|
let x =
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,9 @@ module type S = sig
|
||||||
val get : key -> 'a t -> 'a option
|
val get : key -> 'a t -> 'a option
|
||||||
(** Safe version of {!find} *)
|
(** Safe version of {!find} *)
|
||||||
|
|
||||||
val get_or : key -> 'a t -> or_:'a -> 'a
|
val get_or : key -> 'a t -> default:'a -> 'a
|
||||||
(** [get_or k m ~or_] returns the value associated to [k] if present,
|
(** [get_or k m ~default] returns the value associated to [k] if present,
|
||||||
and returns [or_] otherwise (if [k] doesn't belong in [m])
|
and returns [default] otherwise (if [k] doesn't belong in [m])
|
||||||
@since 0.16 *)
|
@since 0.16 *)
|
||||||
|
|
||||||
val update : key -> ('a option -> 'a option) -> 'a t -> 'a t
|
val update : key -> ('a option -> 'a option) -> 'a t -> 'a t
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue