change argument ordering in CCList.Assoc

This commit is contained in:
Simon Cruanes 2016-11-03 22:03:13 +01:00
parent ad1513e36c
commit 6ff6c51687
2 changed files with 30 additions and 30 deletions

View file

@ -898,17 +898,17 @@ module Assoc = struct
| (y,z)::l' -> | (y,z)::l' ->
if eq x y then z else search_exn eq l' x if eq x y then z else search_exn eq l' x
let get_exn ?(eq=(=)) l x = search_exn eq l x let get_exn ?(eq=(=)) x l = search_exn eq l x
let get ?(eq=(=)) l x = let get ?(eq=(=)) x l =
try Some (search_exn eq l x) try Some (search_exn eq l x)
with Not_found -> None with Not_found -> None
(*$T (*$T
Assoc.get [1, "1"; 2, "2"] 1 = Some "1" Assoc.get 1 [1, "1"; 2, "2"] = Some "1"
Assoc.get [1, "1"; 2, "2"] 2 = Some "2" Assoc.get 2 [1, "1"; 2, "2"] = Some "2"
Assoc.get [1, "1"; 2, "2"] 3 = None Assoc.get 3 [1, "1"; 2, "2"] = None
Assoc.get [] 42 = None Assoc.get 42 [] = None
*) *)
(* search for a binding for [x] in [l], and calls [f x (Some v) rest] (* search for a binding for [x] in [l], and calls [f x (Some v) rest]
@ -921,27 +921,27 @@ module Assoc = struct
then f x (Some y') (List.rev_append acc l') then f x (Some y') (List.rev_append acc l')
else search_set eq ((x',y')::acc) l' x ~f else search_set eq ((x',y')::acc) l' x ~f
let set ?(eq=(=)) l x y = let set ?(eq=(=)) x y l =
search_set eq [] l x search_set eq [] l x
~f:(fun x _ l -> (x,y)::l) ~f:(fun x _ l -> (x,y)::l)
(*$T (*$T
Assoc.set [1,"1"; 2, "2"] 2 "two" |> List.sort Pervasives.compare \ Assoc.set 2 "two" [1,"1"; 2, "2"] |> List.sort Pervasives.compare \
= [1, "1"; 2, "two"] = [1, "1"; 2, "two"]
Assoc.set [1,"1"; 2, "2"] 3 "3" |> List.sort Pervasives.compare \ Assoc.set 3 "3" [1,"1"; 2, "2"] |> List.sort Pervasives.compare \
= [1, "1"; 2, "2"; 3, "3"] = [1, "1"; 2, "2"; 3, "3"]
*) *)
let mem ?(eq=(=)) l x = let mem ?(eq=(=)) x l =
try ignore (search_exn eq l x); true try ignore (search_exn eq l x); true
with Not_found -> false with Not_found -> false
(*$T (*$T
Assoc.mem [1,"1"; 2,"2"; 3, "3"] 1 Assoc.mem 1 [1,"1"; 2,"2"; 3, "3"]
not (Assoc.mem [1,"1"; 2,"2"; 3, "3"] 4) not (Assoc.mem 4 [1,"1"; 2,"2"; 3, "3"])
*) *)
let update ?(eq=(=)) l x ~f = let update ?(eq=(=)) ~f x l =
search_set eq [] l x search_set eq [] l x
~f:(fun x opt_y rest -> ~f:(fun x opt_y rest ->
match f opt_y with match f opt_y with
@ -949,17 +949,17 @@ module Assoc = struct
| Some y' -> (x,y') :: rest) | Some y' -> (x,y') :: rest)
(*$= (*$=
[1,"1"; 2,"22"] \ [1,"1"; 2,"22"] \
(Assoc.update [1,"1"; 2,"2"] 2 \ (Assoc.update 2 [1,"1"; 2,"2"] \
~f:(function Some "2" -> Some "22" | _ -> assert false) |> lsort) ~f:(function Some "2" -> Some "22" | _ -> assert false) |> lsort)
[1,"1"; 3,"3"] \ [1,"1"; 3,"3"] \
(Assoc.update [1,"1"; 2,"2"; 3,"3"] 2 \ (Assoc.update 2 [1,"1"; 2,"2"; 3,"3"] \
~f:(function Some "2" -> None | _ -> assert false) |> lsort) ~f:(function Some "2" -> None | _ -> assert false) |> lsort)
[1,"1"; 2,"2"; 3,"3"] \ [1,"1"; 2,"2"; 3,"3"] \
(Assoc.update [1,"1"; 2,"2"] 3 \ (Assoc.update 3 [1,"1"; 2,"2"] \
~f:(function None -> Some "3" | _ -> assert false) |> lsort) ~f:(function None -> Some "3" | _ -> assert false) |> lsort)
*) *)
let remove ?(eq=(=)) l x = let remove ?(eq=(=)) x l =
search_set eq [] l x search_set eq [] l x
~f:(fun _ opt_y rest -> match opt_y with ~f:(fun _ opt_y rest -> match opt_y with
| None -> l (* keep as is *) | None -> l (* keep as is *)
@ -967,11 +967,11 @@ module Assoc = struct
(*$= (*$=
[1,"1"] \ [1,"1"] \
(Assoc.remove [1,"1"; 2,"2"] 2 |> lsort) (Assoc.remove 2 [1,"1"; 2,"2"] |> lsort)
[1,"1"; 3,"3"] \ [1,"1"; 3,"3"] \
(Assoc.remove [1,"1"; 2,"2"; 3,"3"] 2 |> lsort) (Assoc.remove 2 [1,"1"; 2,"2"; 3,"3"] |> lsort)
[1,"1"; 2,"2"] \ [1,"1"; 2,"2"] \
(Assoc.remove [1,"1"; 2,"2"] 3 |> lsort) (Assoc.remove 3 [1,"1"; 2,"2"] |> lsort)
*) *)
end end

View file

@ -315,29 +315,29 @@ val repeat : int -> 'a t -> 'a t
module Assoc : sig module Assoc : sig
type ('a, 'b) t = ('a*'b) list type ('a, 'b) t = ('a*'b) list
val get : ?eq:('a->'a->bool) -> ('a,'b) t -> 'a -> 'b option val get : ?eq:('a->'a->bool) -> 'a -> ('a,'b) t -> 'b option
(** Find the element *) (** Find the element *)
val get_exn : ?eq:('a->'a->bool) -> ('a,'b) t -> 'a -> 'b val get_exn : ?eq:('a->'a->bool) -> 'a -> ('a,'b) t -> 'b
(** Same as [get] (** Same as [get], but unsafe
@raise Not_found if the element is not present *) @raise Not_found if the element is not present *)
val set : ?eq:('a->'a->bool) -> ('a,'b) t -> 'a -> 'b -> ('a,'b) t val set : ?eq:('a->'a->bool) -> 'a -> 'b -> ('a,'b) t -> ('a,'b) t
(** Add the binding into the list (erase it if already present) *) (** Add the binding into the list (erase it if already present) *)
val mem : ?eq:('a->'a->bool) -> ('a,_) t -> 'a -> bool val mem : ?eq:('a->'a->bool) -> 'a -> ('a,_) t -> bool
(** [mem l x] returns [true] iff [x] is a key in [l] (** [mem x l] returns [true] iff [x] is a key in [l]
@since 0.16 *) @since 0.16 *)
val update : val update :
?eq:('a->'a->bool) -> ('a,'b) t -> 'a -> f:('b option -> 'b option) -> ('a,'b) t ?eq:('a->'a->bool) -> f:('b option -> 'b option) -> 'a -> ('a,'b) t -> ('a,'b) t
(** [update l k ~f] updates [l] on the key [k], by calling [f (get l k)] (** [update k ~f l] updates [l] on the key [k], by calling [f (get l k)]
and removing [k] if it returns [None], mapping [k] to [v'] if it and removing [k] if it returns [None], mapping [k] to [v'] if it
returns [Some v'] returns [Some v']
@since 0.16 *) @since 0.16 *)
val remove : ?eq:('a->'a->bool) -> ('a,'b) t -> 'a -> ('a,'b) t val remove : ?eq:('a->'a->bool) -> 'a -> ('a,'b) t -> ('a,'b) t
(** [remove l k] removes the first occurrence of [k] from [l]. (** [remove x l] removes the first occurrence of [k] from [l].
@since 0.17 *) @since 0.17 *)
end end