Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Stream.group #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions streaming/Stream.ml
Original file line number Diff line number Diff line change
Expand Up @@ -396,20 +396,23 @@ let split ~by:pred self =
in
{ stream }



let group ?equal:(_ =Pervasives.(=)) self =
let group ~break self =
let stream (Sink k) =
let push r x =
k.push r x
let send r xs = k.push r (of_list (List.rev xs)) in
let init () = (k.init (), []) in
let push (r, acc) x =
match acc with
| [] -> (r, [x])
| h :: _ ->
if break h x then (send r acc, [x])
else (r, x :: acc)
in
self.stream (Sink { k with push })
let stop (r, acc) = k.stop (send r acc) in
let full (r, _) = k.full r in
self.stream (Sink { init; push; full; stop })
in
{ stream }




(* IO *)

let of_file path =
Expand Down
6 changes: 3 additions & 3 deletions streaming/Streaming.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1120,9 +1120,9 @@ module Stream : sig
val partition : int -> 'a t -> 'a t t
(** [partition n] partitions the stream into sub-streams of size [n]. *)

(* TODO *)
(* val group : by *)

val group : break:('a -> 'a -> bool) -> 'a t -> 'a t t
(** [group ~break stream] splits [stream] each time two consecutive
elements [x] and [y] verify [break x y]. *)

(* TODO: Add variants for splitting once. Consider renaming: divide. *)
(* split, partition, divide, etc is too confusing. *)
Expand Down
12 changes: 12 additions & 0 deletions tests/Stream_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,18 @@ let () =
~actual:S.(to_list (map to_list (partition 0 (0-<5))));
];

let t = T.test T.(list (list int)) ~verbose in
T.group "Stream.group" [
t "empty" ~expected:[[]]
~actual:S.(to_list (map to_list (group ~break:( <> ) empty)));
t "of_list" ~expected:[[1;1];[2];[3;3]]
~actual:S.(to_list (map to_list (group ~break:( <> ) (of_list [1;1;2;3;3]))));
t "of_list, mod" ~expected:[[1;3];[2];[1;3]]
~actual:S.(to_list (map to_list (group ~break:(fun x y -> x mod 2 <> y mod 2) (of_list [1;3;2;1;3]))));
t "repeat,concat" ~expected:[[1;1];[2;2;2]]
~actual:S.(to_list (map to_list (group ~break:( <> ) (concat (repeat ~times:2 1) (repeat ~times:3 2)) )));
];

let t = T.test T.(list int) ~verbose in
T.group "Stream.interpose" [
t "empty" ~expected:[]
Expand Down