aoc2020/set_ext.ml

15 lines
409 B
OCaml

module type S = sig
include Set.S
val exists_opt: (elt -> 'a option) -> t -> 'a option
end
module Make(Elt: Set.OrderedType) = struct
include Set.Make(Elt)
let exists_opt (type a) (f: elt -> a option) set =
let exception Found of a in
let f' x = Option.iter (fun x -> raise (Found x)) (f x) in
match iter f' set with
| exception Found x -> Some x
| _ -> None
end