mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 11:15:32 -05:00
more natural functors to adapt Set and Map
This commit is contained in:
parent
1a6129fc36
commit
4f06874e39
2 changed files with 27 additions and 30 deletions
30
sequence.ml
30
sequence.ml
|
|
@ -28,6 +28,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
(** Sequence abstract iterator type *)
|
(** Sequence abstract iterator type *)
|
||||||
type 'a t = ('a -> unit) -> unit
|
type 'a t = ('a -> unit) -> unit
|
||||||
|
|
||||||
|
type 'a sequence = 'a t
|
||||||
|
|
||||||
type (+'a, +'b) t2 = ('a -> 'b -> unit) -> unit
|
type (+'a, +'b) t2 = ('a -> 'b -> unit) -> unit
|
||||||
(** Sequence of pairs of values of type ['a] and ['b]. *)
|
(** Sequence of pairs of values of type ['a] and ['b]. *)
|
||||||
|
|
||||||
|
|
@ -539,16 +541,13 @@ let to_set (type s) (type v) m seq =
|
||||||
|
|
||||||
module Set = struct
|
module Set = struct
|
||||||
module type S = sig
|
module type S = sig
|
||||||
type set
|
include Set.S
|
||||||
include Set.S with type t := set
|
val of_seq : elt sequence -> t
|
||||||
val of_seq : elt t -> set
|
val to_seq : t -> elt sequence
|
||||||
val to_seq : set -> elt t
|
|
||||||
end
|
end
|
||||||
|
|
||||||
(** Create an enriched Set module from the given one *)
|
(** 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
|
module Adapt(X : Set.S) = struct
|
||||||
type set = X.t
|
|
||||||
|
|
||||||
let to_seq set = from_iter (fun k -> X.iter k set)
|
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
|
let of_seq seq = fold (fun set x -> X.add x set) X.empty seq
|
||||||
|
|
@ -557,7 +556,7 @@ module Set = struct
|
||||||
end
|
end
|
||||||
|
|
||||||
(** Functor to build an extended Set module from an ordered type *)
|
(** Functor to build an extended Set module from an ordered type *)
|
||||||
module Make(X : Set.OrderedType) : S with type elt = X.t = struct
|
module Make(X : Set.OrderedType) = struct
|
||||||
module MySet = Set.Make(X)
|
module MySet = Set.Make(X)
|
||||||
include Adapt(MySet)
|
include Adapt(MySet)
|
||||||
end
|
end
|
||||||
|
|
@ -567,18 +566,15 @@ end
|
||||||
|
|
||||||
module Map = struct
|
module Map = struct
|
||||||
module type S = sig
|
module type S = sig
|
||||||
type +'a map
|
include Map.S
|
||||||
include Map.S with type 'a t := 'a map
|
val to_seq : 'a t -> (key * 'a) sequence
|
||||||
val to_seq : 'a map -> (key * 'a) t
|
val of_seq : (key * 'a) sequence -> 'a t
|
||||||
val of_seq : (key * 'a) t -> 'a map
|
val keys : 'a t -> key sequence
|
||||||
val keys : 'a map -> key t
|
val values : 'a t -> 'a sequence
|
||||||
val values : 'a map -> 'a t
|
|
||||||
end
|
end
|
||||||
|
|
||||||
(** Adapt a pre-existing Map module to make it sequence-aware *)
|
(** Adapt a pre-existing Map module to make it sequence-aware *)
|
||||||
module Adapt(M : Map.S) : S with type key = M.key and type 'a map = 'a M.t = struct
|
module Adapt(M : Map.S) = struct
|
||||||
type 'a map = 'a M.t
|
|
||||||
|
|
||||||
let to_seq m = from_iter (fun k -> M.iter (fun x y -> k (x,y)) m)
|
let to_seq m = from_iter (fun k -> M.iter (fun x y -> k (x,y)) m)
|
||||||
|
|
||||||
let of_seq seq = fold (fun m (k,v) -> M.add k v m) M.empty seq
|
let of_seq seq = fold (fun m (k,v) -> M.add k v m) M.empty seq
|
||||||
|
|
|
||||||
27
sequence.mli
27
sequence.mli
|
|
@ -42,6 +42,8 @@ type +'a t = ('a -> unit) -> unit
|
||||||
(** Sequence abstract iterator type, representing a finite sequence of
|
(** Sequence abstract iterator type, representing a finite sequence of
|
||||||
values of type ['a]. *)
|
values of type ['a]. *)
|
||||||
|
|
||||||
|
type +'a sequence = 'a t
|
||||||
|
|
||||||
type (+'a, +'b) t2 = ('a -> 'b -> unit) -> unit
|
type (+'a, +'b) t2 = ('a -> 'b -> unit) -> unit
|
||||||
(** Sequence of pairs of values of type ['a] and ['b]. *)
|
(** Sequence of pairs of values of type ['a] and ['b]. *)
|
||||||
|
|
||||||
|
|
@ -70,7 +72,8 @@ val forever : (unit -> 'b) -> 'b t
|
||||||
(** Sequence that calls the given function to produce elements *)
|
(** Sequence that calls the given function to produce elements *)
|
||||||
|
|
||||||
val cycle : 'a t -> 'a t
|
val cycle : 'a t -> 'a t
|
||||||
(** Cycle forever through the given sequence. *)
|
(** Cycle forever through the given sequence. Assume the
|
||||||
|
given sequence can be traversed any amount of times (not transient). *)
|
||||||
|
|
||||||
(** {2 Consume a sequence} *)
|
(** {2 Consume a sequence} *)
|
||||||
|
|
||||||
|
|
@ -286,14 +289,13 @@ val to_set : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'b
|
||||||
|
|
||||||
module Set : sig
|
module Set : sig
|
||||||
module type S = sig
|
module type S = sig
|
||||||
type set
|
include Set.S
|
||||||
include Set.S with type t := set
|
val of_seq : elt sequence -> t
|
||||||
val of_seq : elt t -> set
|
val to_seq : t -> elt sequence
|
||||||
val to_seq : set -> elt t
|
|
||||||
end
|
end
|
||||||
|
|
||||||
(** Create an enriched Set module from the given one *)
|
(** 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
|
module Adapt(X : Set.S) : S with type elt = X.elt and type t = X.t
|
||||||
|
|
||||||
(** Functor to build an extended Set module from an ordered type *)
|
(** Functor to build an extended Set module from an ordered type *)
|
||||||
module Make(X : Set.OrderedType) : S with type elt = X.t
|
module Make(X : Set.OrderedType) : S with type elt = X.t
|
||||||
|
|
@ -303,16 +305,15 @@ end
|
||||||
|
|
||||||
module Map : sig
|
module Map : sig
|
||||||
module type S = sig
|
module type S = sig
|
||||||
type +'a map
|
include Map.S
|
||||||
include Map.S with type 'a t := 'a map
|
val to_seq : 'a t -> (key * 'a) sequence
|
||||||
val to_seq : 'a map -> (key * 'a) t
|
val of_seq : (key * 'a) sequence -> 'a t
|
||||||
val of_seq : (key * 'a) t -> 'a map
|
val keys : 'a t -> key sequence
|
||||||
val keys : 'a map -> key t
|
val values : 'a t -> 'a sequence
|
||||||
val values : 'a map -> 'a t
|
|
||||||
end
|
end
|
||||||
|
|
||||||
(** Adapt a pre-existing Map module to make it sequence-aware *)
|
(** Adapt a pre-existing Map module to make it sequence-aware *)
|
||||||
module Adapt(M : Map.S) : S with type key = M.key and type 'a map = 'a M.t
|
module Adapt(M : Map.S) : S with type key = M.key and type 'a t = 'a M.t
|
||||||
|
|
||||||
(** Create an enriched Map module, with sequence-aware functions *)
|
(** Create an enriched Map module, with sequence-aware functions *)
|
||||||
module Make(V : Map.OrderedType) : S with type key = V.t
|
module Make(V : Map.OrderedType) : S with type key = V.t
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue