diff --git a/sequence.ml b/sequence.ml index 34f26bc..915ed6d 100644 --- a/sequence.ml +++ b/sequence.ml @@ -131,109 +131,99 @@ let is_empty seq = try seq (fun _ -> raise Exit); true with Exit -> false -module List = - struct - let of_seq seq = List.rev (fold (fun y x -> x::y) [] seq) +let to_list seq = List.rev (fold (fun y x -> x::y) [] seq) - let of_rev_seq seq = fold (fun y x -> x :: y) [] seq +let to_rev_list seq = fold (fun y x -> x :: y) [] seq + (** Get the list of the reversed sequence (more efficient) *) - let to_seq l = from_iter (fun k -> List.iter k l) - end +let of_list l = from_iter (fun k -> List.iter k l) -module Array = - struct - let of_seq seq = - (* intermediate list... *) - let l = List.of_rev_seq seq in - let a = Array.of_list l in - (* reverse array *) - let n = Array.length a in - for i = 0 to (n-1) / 2 do - let tmp = a.(i) in - a.(i) <- a.(n-i-1); - a.(n-i-1) <- tmp; - done; - a +let to_array seq = + (* intermediate list... *) + let l = to_rev_list seq in + let a = Array.of_list l in + (* reverse array *) + let n = Array.length a in + for i = 0 to (n-1) / 2 do + let tmp = a.(i) in + a.(i) <- a.(n-i-1); + a.(n-i-1) <- tmp; + done; + a - let to_seq a = from_iter (fun k -> Array.iter k a) +let of_array a = from_iter (fun k -> Array.iter k a) - let slice a i j = - assert (i >= 0 && j < Array.length a); - fun k -> - for idx = i to j do - k a.(idx); (* iterate on sub-array *) - done - end +(** [array_slice a i j] Sequence of elements whose indexes range + from [i] to [j] *) +let array_slice a i j = + assert (i >= 0 && j < Array.length a); + fun k -> + for idx = i to j do + k a.(idx); (* iterate on sub-array *) + done -module Stack = - struct - let push_seq s seq = iter (fun x -> Stack.push x s) seq +(** Push elements of the sequence on the stack *) +let to_stack s seq = iter (fun x -> Stack.push x s) seq - let to_seq s = from_iter (fun k -> Stack.iter k s) - end +(** Sequence of elements of the stack (same order as [Stack.iter]) *) +let of_stack s = from_iter (fun k -> Stack.iter k s) -module Queue = - struct - let push_seq q seq = iter (fun x -> Queue.push x q) seq - let to_seq q = from_iter (fun k -> Queue.iter k q) - end +(** Push elements of the sequence into the queue *) +let to_queue q seq = iter (fun x -> Queue.push x q) seq -module Hashtbl = - struct - let add_seq h seq = - iter (fun (k,v) -> Hashtbl.add h k v) seq +(** Sequence of elements contained in the queue, FIFO order *) +let of_queue q = from_iter (fun k -> Queue.iter k q) - let replace_seq h seq = - iter (fun (k,v) -> Hashtbl.replace h k v) seq +let hashtbl_add h seq = + iter (fun (k,v) -> Hashtbl.add h k v) seq - let of_seq seq = - let h = Hashtbl.create 3 in - replace_seq h seq; - h +let hashtbl_replace h seq = + iter (fun (k,v) -> Hashtbl.replace h k v) seq - let to_seq h = - from_iter (fun k -> Hashtbl.iter (fun a b -> k (a, b)) h) +let to_hashtbl seq = + let h = Hashtbl.create 3 in + hashtbl_replace h seq; + h - let keys h = - from_iter (fun k -> Hashtbl.iter (fun a b -> k a) h) +let of_hashtbl h = + from_iter (fun k -> Hashtbl.iter (fun a b -> k (a, b)) h) - let values h = - from_iter (fun k -> Hashtbl.iter (fun a b -> k b) h) - end +let hashtbl_keys h = + from_iter (fun k -> Hashtbl.iter (fun a b -> k a) h) -module String = - struct - let to_seq s = from_iter (fun k -> String.iter k s) +let hashtbl_values h = + from_iter (fun k -> Hashtbl.iter (fun a b -> k b) h) + +let of_str s = from_iter (fun k -> String.iter k s) - let of_seq seq = - let b = Buffer.create 64 in - iter (fun c -> Buffer.add_char b c) seq; - Buffer.contents b +let to_str seq = + let b = Buffer.create 64 in + iter (fun c -> Buffer.add_char b c) seq; + Buffer.contents b - let of_in ic = - from_iter (fun k -> - try while true do - let c = input_char ic in k c - done with End_of_file -> ()) - end +let of_in_channel ic = + from_iter (fun k -> + try while true do + let c = input_char ic in k c + done with End_of_file -> ()) -module Int = - struct - let range ~start ~stop = - fun k -> - for i = start to stop do k i done - end +(** Iterator on integers in [start...stop] by steps 1 *) +let int_range ~start ~stop = + fun k -> + for i = start to stop do k i done -(** Iterate on sets. The functor must be instantiated with a set type *) -module Set(S : Set.S) = - struct - type set = S.t - type elt = S.elt +(** Convert the given set to a sequence. The set module must be provided. *) +let of_set (type s) (type v) m set = + let module S = (val m : Set.S with type t = s and type elt = v) in + from_iter + (fun k -> S.iter k set) - let to_seq set = from_iter (fun k -> S.iter k set) - - let of_seq seq = fold (fun set x -> S.add x set) S.empty seq - end +(** Convert the sequence to a set, given the proper set module *) +let to_set (type s) (type v) m seq = + let module S = (val m : Set.S with type t = s and type elt = v) in + fold + (fun set x -> S.add x set) + S.empty seq (** Iterate on maps. The functor must be instantiated with a map type *) module Map(M : Map.S) = diff --git a/sequence.mli b/sequence.mli index ad44c57..e9e50f9 100644 --- a/sequence.mli +++ b/sequence.mli @@ -94,90 +94,65 @@ val rev : 'a t -> 'a t (** {2 Basic data structures converters} *) -module List : - sig - val of_seq : 'a t -> 'a list +val to_list : 'a t -> 'a list - val of_rev_seq : 'a t -> 'a list - (** Get the list of the reversed sequence (more efficient) *) +val to_rev_list : 'a t -> 'a list + (** Get the list of the reversed sequence (more efficient) *) - val to_seq : 'a list -> 'a t - end +val of_list : 'a list -> 'a t -module Array : - sig - val of_seq : 'a t -> 'a array +val to_array : 'a t -> 'a array - val to_seq : 'a array -> 'a t +val of_array : 'a array -> 'a t - val slice : 'a array -> int -> int -> 'a t - (** [slice a i j] Sequence of elements whose indexes range - from [i] to [j] *) - end +val array_slice : 'a array -> int -> int -> 'a t + (** [array_slice a i j] Sequence of elements whose indexes range + from [i] to [j] *) -module Stack : - sig - val push_seq : 'a Stack.t -> 'a t -> unit - (** Push elements of the sequence on the stack *) +val to_stack : 'a Stack.t -> 'a t -> unit + (** Push elements of the sequence on the stack *) - val to_seq : 'a Stack.t -> 'a t - (** Sequence of elements of the stack (same order as [Stack.iter]) *) - end +val of_stack : 'a Stack.t -> 'a t + (** Sequence of elements of the stack (same order as [Stack.iter]) *) -module Queue : - sig - val push_seq : 'a Queue.t -> 'a t -> unit - (** Push elements of the sequence into the queue *) +val to_queue : 'a Queue.t -> 'a t -> unit + (** Push elements of the sequence into the queue *) - val to_seq : 'a Queue.t -> 'a t - (** Sequence of elements contained in the queue, FIFO order *) - end +val of_queue : 'a Queue.t -> 'a t + (** Sequence of elements contained in the queue, FIFO order *) -module Hashtbl : - sig - val add_seq : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unit - (** Add elements of the sequence to the hashtable, with - Hashtbl.add *) +val hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unit + (** Add elements of the sequence to the hashtable, with + Hashtbl.add *) - val replace_seq : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unit - (** Add elements of the sequence to the hashtable, with - Hashtbl.replace (erases conflicting bindings) *) +val hashtbl_replace : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unit + (** Add elements of the sequence to the hashtable, with + Hashtbl.replace (erases conflicting bindings) *) - val of_seq : ('a * 'b) t -> ('a, 'b) Hashtbl.t - (** Build a hashtable from a sequence *) +val to_hashtbl :('a * 'b) t -> ('a, 'b) Hashtbl.t + (** Build a hashtable from a sequence of key/value pairs *) - val to_seq : ('a, 'b) Hashtbl.t -> ('a * 'b) t - (** Sequence of key/value pairs from the hashtable *) +val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) t + (** Sequence of key/value pairs from the hashtable *) - val keys : ('a, 'b) Hashtbl.t -> 'a t - val values : ('a, 'b) Hashtbl.t -> 'b t - end +val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a t +val hashtbl_values : ('a, 'b) Hashtbl.t -> 'b t -module String : - sig - val to_seq : string -> char t - val of_seq : char t -> string +val of_str : string -> char t +val to_str : char t -> string +val of_in_channel : in_channel -> char t - val of_in : in_channel -> char t - end +val int_range : start:int -> stop:int -> int t + (** Iterator on integers in [start...stop] by steps 1 *) -(** Sequences of ints *) -module Int : - sig - val range : start:int -> stop:int -> int t - (** Iterator on [start...stop] by steps 1 *) - end +val of_set : (module Set.S with type elt = 'a and type t = 'b) -> 'b -> 'a t + (** Convert the given set to a sequence. The set module must be provided. *) -(** Iterate on sets. The functor must be instantiated with a set type *) -module Set(S : Set.S) : - sig - type set = S.t - type elt = S.elt - - val to_seq : set -> elt t +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 *) - val of_seq : elt t -> set - end +val of_map : (module Map.S with type key = 'a and type t = 'b) -> 'b -> ('a * 'c) t + (** Convert the Map to the sequence of its key/values *) (** Iterate on maps. The functor must be instantiated with a map type *) module Map(M : Map.S) :