aoc2020/set.ml

18 lines
469 B
OCaml

module type OrderedType = Stdlib.Set.OrderedType
module type S = sig
include Stdlib.Set.S
val exists_opt: (elt -> 'a option) -> t -> 'a option
end
module Make(Elt: OrderedType) = struct
include Stdlib.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