aoc2020/set.ml

18 lines
469 B
OCaml
Raw Normal View History

2020-12-06 04:15:56 -05:00
module type OrderedType = Stdlib.Set.OrderedType
2020-12-03 09:11:29 -05:00
module type S = sig
2020-12-06 04:15:56 -05:00
include Stdlib.Set.S
2020-12-03 09:11:29 -05:00
val exists_opt: (elt -> 'a option) -> t -> 'a option
end
2020-12-06 04:15:56 -05:00
module Make(Elt: OrderedType) = struct
include Stdlib.Set.Make(Elt)
2020-12-03 09:11:29 -05:00
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