diff --git a/enum.ml b/enum.ml index 58e94068..8a72da94 100644 --- a/enum.ml +++ b/enum.ml @@ -289,7 +289,7 @@ let zipIndex enum = (** {2 Complex combinators} *) (** Pick elements fairly in each sub-enum *) -let round_robin enum = +let merge enum = (* list of sub-enums *) let l = fold (fun acc x -> x::acc) [] enum in let l = List.rev l in @@ -309,6 +309,11 @@ let round_robin enum = x in next +(** Assuming subsequences are sorted in increasing order, merge them + into an increasing sequence *) +let merge_sorted ?(cmp=compare) enum = + failwith "not implemented" + (** {3 Mutable double-linked list, similar to {! Deque.t} *) module MList = struct type 'a t = 'a node option ref @@ -362,7 +367,7 @@ let persistent gen = (* done recursing through the generator *) MList.to_enum l -let tee ?(n=2) enum = +let round_robin ?(n=2) enum = fun () -> (* array of queues, together with their index *) let qs = Array.init n (fun i -> Queue.create ()) in @@ -399,7 +404,7 @@ let tee ?(n=2) enum = (** Duplicate the enum into [n] generators (default 2). The generators share the same underlying instance of the enum, so the optimal case is when they are consumed evenly *) -let dup ?(n=2) enum = +let tee ?(n=2) enum = fun () -> (* array of queues, together with their index *) let qs = Array.init n (fun i -> Queue.create ()) in diff --git a/enum.mli b/enum.mli index 66ff9bb9..db87070e 100644 --- a/enum.mli +++ b/enum.mli @@ -139,20 +139,24 @@ val zipIndex : 'a t -> (int * 'a) t (** {2 Complex combinators} *) -val round_robin : 'a t t -> 'a t +val merge : 'a t t -> 'a t (** Pick elements fairly in each sub-enum. The given enum must be finite (not its elements, though). *) +val merge_sorted : ?cmp:('a -> 'a -> int) -> 'a t t -> 'a t + (** Assuming subsequences are sorted in increasing order, merge them + into an increasing sequence *) + val persistent : 'a generator -> 'a t (** Store content of the generator in memory, to be able to iterate on it several times later *) -val tee : ?n:int -> 'a t -> 'a generator t +val round_robin : ?n:int -> 'a t -> 'a generator t (** Split the enum into [n] generators in a fair way. Elements with [index = k mod n] with go to the k-th enum. [n] defaults value is 2. *) -val dup : ?n:int -> 'a t -> 'a generator t +val tee : ?n:int -> 'a t -> 'a generator t (** Duplicate the enum into [n] generators (default 2). The generators share the same underlying instance of the enum, so the optimal case is when they are consumed evenly *) diff --git a/tests/test_enum.ml b/tests/test_enum.ml index cdedd461..064359f1 100644 --- a/tests/test_enum.ml +++ b/tests/test_enum.ml @@ -51,9 +51,9 @@ let test_filterMap () = OUnit.assert_equal ["2"; "4"; "6"; "8"; "10"] (Enum.to_list e); () -let test_round_robin () = +let test_merge () = let e = Enum.of_list [1--3; 4--6; 7--9] in - let e' = Enum.round_robin e in + let e' = Enum.merge e in OUnit.assert_equal [1;4;7;2;5;8;3;6;9] (Enum.to_list e'); () @@ -69,8 +69,8 @@ let test_persistent () = OUnit.assert_equal [0;1;2;3;4;5] (Enum.to_list e); () -let test_tee () = - let e = Enum.tee ~n:2 (1--10) in +let test_round_robin () = + let e = Enum.round_robin ~n:2 (1--10) in let e = Enum.map Enum.persistent e in let l = Enum.to_list e in match l with @@ -79,8 +79,8 @@ let test_tee () = OUnit.assert_equal [2;4;6;8;10] (Enum.to_list b) | _ -> OUnit.assert_failure "wrong list lenght" -let test_strong_tee () = - let e = Enum.tee ~n:3 (1 -- 999) in +let test_big_rr () = + let e = Enum.round_robin ~n:3 (1 -- 999) in let l = Enum.to_list e in let l' = List.map Enum.Gen.length l in OUnit.assert_equal [333;333;333] l'; @@ -113,10 +113,10 @@ let suite = "test_flatMap" >:: test_flatMap; "test_zip" >:: test_zip; "test_filterMap" >:: test_filterMap; - "test_round_robin" >:: test_round_robin; + "test_merge" >:: test_merge; "test_persistent" >:: test_persistent; - "test_tee" >:: test_tee; - "test_strong_tee" >:: test_strong_tee; + "test_round_robin" >:: test_round_robin; + "test_big_rr" >:: test_big_rr; "test_interleave" >:: test_interleave; "test_intersperse" >:: test_intersperse; "test_product" >:: test_product;