add CCList.Assoc.remove

This commit is contained in:
Simon Cruanes 2016-03-08 15:50:11 +01:00
parent 8094a31604
commit d6487a02a0
2 changed files with 19 additions and 0 deletions

View file

@ -849,6 +849,21 @@ module Assoc = struct
(Assoc.update [1,"1"; 2,"2"] 3 \
~f:(function None -> Some "3" | _ -> assert false) |> lsort)
*)
let remove ?(eq=(=)) l x =
search_set eq [] l x
~f:(fun _ opt_y rest -> match opt_y with
| None -> l (* keep as is *)
| Some _ -> rest)
(*$=
[1,"1"] \
(Assoc.remove [1,"1"; 2,"2"] 2 |> lsort)
[1,"1"; 3,"3"] \
(Assoc.remove [1,"1"; 2,"2"; 3,"3"] 2 |> lsort)
[1,"1"; 2,"2"] \
(Assoc.remove [1,"1"; 2,"2"] 3 |> lsort)
*)
end
(** {2 Zipper} *)

View file

@ -294,6 +294,10 @@ module Assoc : sig
and removing [k] if it returns [None], mapping [k] to [v'] if it
returns [Some v']
@since 0.16 *)
val remove : ?eq:('a->'a->bool) -> ('a,'b) t -> 'a -> ('a,'b) t
(** [remove l k] removes the first occurrence of [k] from [l].
@since NEXT_RELEASE *)
end
(** {2 Zipper} *)