From 43f4f25ed7e108ebfa1cc8a45f41bc7d98fda1bd Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 7 Mar 2013 19:52:04 +0100 Subject: [PATCH] some conversion functions that use Sequence.t2 --- sequence.ml | 12 ++++++++++++ sequence.mli | 10 +++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/sequence.ml b/sequence.ml index 16d8da7..f46558e 100644 --- a/sequence.ml +++ b/sequence.ml @@ -362,6 +362,10 @@ let of_array_i a = for i = 0 to Array.length a - 1 do k (i, a.(i)) done in from_iter seq +let of_array2 a = + fun k -> + for i = 0 to Array.length a - 1 do k i a.(i) done + (** [array_slice a i j] Sequence of elements whose indexes range from [i] to [j] *) let array_slice a i j = @@ -410,9 +414,17 @@ let to_hashtbl seq = hashtbl_replace h seq; h +let to_hashtbl2 seq2 = + let h = Hashtbl.create 3 in + seq2 (fun k v -> Hashtbl.replace h k v); + h + let of_hashtbl h = from_iter (fun k -> Hashtbl.iter (fun a b -> k (a, b)) h) +let of_hashtbl2 h = + fun k -> Hashtbl.iter k h + let hashtbl_keys h = from_iter (fun k -> Hashtbl.iter (fun a b -> k a) h) diff --git a/sequence.mli b/sequence.mli index dae0321..a1da7bc 100644 --- a/sequence.mli +++ b/sequence.mli @@ -183,6 +183,8 @@ val of_array : 'a array -> 'a t val of_array_i : 'a array -> (int * 'a) t (** Elements of the array, with their index *) +val of_array2 : 'a array -> (int, 'a) t2 + val array_slice : 'a array -> int -> int -> 'a t (** [array_slice a i j] Sequence of elements whose indexes range from [i] to [j] *) @@ -213,12 +215,18 @@ 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 to_hashtbl :('a * 'b) t -> ('a, 'b) Hashtbl.t +val to_hashtbl : ('a * 'b) t -> ('a, 'b) Hashtbl.t + (** Build a hashtable from a sequence of key/value pairs *) + +val to_hashtbl2 : ('a, 'b) t2 -> ('a, 'b) Hashtbl.t (** Build a hashtable from a sequence of key/value pairs *) val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) t (** Sequence of key/value pairs from the hashtable *) +val of_hashtbl2 : ('a, 'b) Hashtbl.t -> ('a, 'b) t2 + (** Sequence of key/value pairs from the hashtable *) + val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a t val hashtbl_values : ('a, 'b) Hashtbl.t -> 'b t