functorial interface for Set too

This commit is contained in:
Simon Cruanes 2013-02-07 16:16:23 +01:00
parent 07cc9df645
commit af0c94dab4
2 changed files with 49 additions and 2 deletions

View file

@ -228,7 +228,36 @@ let to_set (type s) (type v) m seq =
(fun set x -> S.add x set) (fun set x -> S.add x set)
S.empty seq S.empty seq
(** Conversion between maps and sequences. *) (** {2 Functorial conversions between sets and sequences} *)
module Set = struct
module type S = sig
type set
include Set.S with type t := set
val of_seq : elt t -> set
val to_seq : set -> elt t
end
(** Create an enriched Set module from the given one *)
module Adapt(X : Set.S) : S with type elt = X.elt and type set = X.t = struct
type set = X.t
let to_seq set = from_iter (fun k -> X.iter k set)
let of_seq seq = fold (fun set x -> X.add x set) X.empty seq
include X
end
(** Functor to build an extended Set module from an ordered type *)
module Make(X : Set.OrderedType) : S with type elt = X.t = struct
module MySet = Set.Make(X)
include Adapt(MySet)
end
end
(** {2 Conversion between maps and sequences.} *)
module Map = struct module Map = struct
module type S = sig module type S = sig
type +'a map type +'a map

View file

@ -151,7 +151,25 @@ val of_set : (module Set.S with type elt = 'a and type t = 'b) -> 'b -> 'a t
val to_set : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'b val to_set : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'b
(** Convert the sequence to a set, given the proper set module *) (** Convert the sequence to a set, given the proper set module *)
(** Conversion between maps and sequences. *) (** {2 Functorial conversions between sets and sequences} *)
module Set : sig
module type S = sig
type set
include Set.S with type t := set
val of_seq : elt t -> set
val to_seq : set -> elt t
end
(** Create an enriched Set module from the given one *)
module Adapt(X : Set.S) : S with type elt = X.elt and type set = X.t
(** Functor to build an extended Set module from an ordered type *)
module Make(X : Set.OrderedType) : S with type elt = X.t
end
(** {2 Conversion between maps and sequences.} *)
module Map : sig module Map : sig
module type S = sig module type S = sig
type +'a map type +'a map