renamed some operations in Enum

This commit is contained in:
Simon Cruanes 2013-03-20 13:58:50 +01:00
parent fb75b7299b
commit fac35bf61c
3 changed files with 24 additions and 15 deletions

11
enum.ml
View file

@ -289,7 +289,7 @@ let zipIndex enum =
(** {2 Complex combinators} *) (** {2 Complex combinators} *)
(** Pick elements fairly in each sub-enum *) (** Pick elements fairly in each sub-enum *)
let round_robin enum = let merge enum =
(* list of sub-enums *) (* list of sub-enums *)
let l = fold (fun acc x -> x::acc) [] enum in let l = fold (fun acc x -> x::acc) [] enum in
let l = List.rev l in let l = List.rev l in
@ -309,6 +309,11 @@ let round_robin enum =
x x
in next 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} *) (** {3 Mutable double-linked list, similar to {! Deque.t} *)
module MList = struct module MList = struct
type 'a t = 'a node option ref type 'a t = 'a node option ref
@ -362,7 +367,7 @@ let persistent gen =
(* done recursing through the generator *) (* done recursing through the generator *)
MList.to_enum l MList.to_enum l
let tee ?(n=2) enum = let round_robin ?(n=2) enum =
fun () -> fun () ->
(* array of queues, together with their index *) (* array of queues, together with their index *)
let qs = Array.init n (fun i -> Queue.create ()) in 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 (** Duplicate the enum into [n] generators (default 2). The generators
share the same underlying instance of the enum, so the optimal case is share the same underlying instance of the enum, so the optimal case is
when they are consumed evenly *) when they are consumed evenly *)
let dup ?(n=2) enum = let tee ?(n=2) enum =
fun () -> fun () ->
(* array of queues, together with their index *) (* array of queues, together with their index *)
let qs = Array.init n (fun i -> Queue.create ()) in let qs = Array.init n (fun i -> Queue.create ()) in

View file

@ -139,20 +139,24 @@ val zipIndex : 'a t -> (int * 'a) t
(** {2 Complex combinators} *) (** {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 (** Pick elements fairly in each sub-enum. The given enum
must be finite (not its elements, though). *) 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 val persistent : 'a generator -> 'a t
(** Store content of the generator in memory, to be able to iterate on it (** Store content of the generator in memory, to be able to iterate on it
several times later *) 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 (** 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 [index = k mod n] with go to the k-th enum. [n] defaults value
is 2. *) 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 (** Duplicate the enum into [n] generators (default 2). The generators
share the same underlying instance of the enum, so the optimal case is share the same underlying instance of the enum, so the optimal case is
when they are consumed evenly *) when they are consumed evenly *)

View file

@ -51,9 +51,9 @@ let test_filterMap () =
OUnit.assert_equal ["2"; "4"; "6"; "8"; "10"] (Enum.to_list e); 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.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'); 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); OUnit.assert_equal [0;1;2;3;4;5] (Enum.to_list e);
() ()
let test_tee () = let test_round_robin () =
let e = Enum.tee ~n:2 (1--10) in let e = Enum.round_robin ~n:2 (1--10) in
let e = Enum.map Enum.persistent e in let e = Enum.map Enum.persistent e in
let l = Enum.to_list e in let l = Enum.to_list e in
match l with match l with
@ -79,8 +79,8 @@ let test_tee () =
OUnit.assert_equal [2;4;6;8;10] (Enum.to_list b) OUnit.assert_equal [2;4;6;8;10] (Enum.to_list b)
| _ -> OUnit.assert_failure "wrong list lenght" | _ -> OUnit.assert_failure "wrong list lenght"
let test_strong_tee () = let test_big_rr () =
let e = Enum.tee ~n:3 (1 -- 999) in let e = Enum.round_robin ~n:3 (1 -- 999) in
let l = Enum.to_list e in let l = Enum.to_list e in
let l' = List.map Enum.Gen.length l in let l' = List.map Enum.Gen.length l in
OUnit.assert_equal [333;333;333] l'; OUnit.assert_equal [333;333;333] l';
@ -113,10 +113,10 @@ let suite =
"test_flatMap" >:: test_flatMap; "test_flatMap" >:: test_flatMap;
"test_zip" >:: test_zip; "test_zip" >:: test_zip;
"test_filterMap" >:: test_filterMap; "test_filterMap" >:: test_filterMap;
"test_round_robin" >:: test_round_robin; "test_merge" >:: test_merge;
"test_persistent" >:: test_persistent; "test_persistent" >:: test_persistent;
"test_tee" >:: test_tee; "test_round_robin" >:: test_round_robin;
"test_strong_tee" >:: test_strong_tee; "test_big_rr" >:: test_big_rr;
"test_interleave" >:: test_interleave; "test_interleave" >:: test_interleave;
"test_intersperse" >:: test_intersperse; "test_intersperse" >:: test_intersperse;
"test_product" >:: test_product; "test_product" >:: test_product;