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

Added Stream.bufferTime see #132 #133

Open
wants to merge 1 commit 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
25 changes: 25 additions & 0 deletions Libs/Hopac/Stream.fs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,31 @@ module Stream =
if n < 1 then failwith "buffer: n < 1" else
buffer' 0 (Array.zeroCreate n) xs |> memo

let rec bufferTime' n (b: ResizeArray<_>) timeOut xs clock =
Alt.tryIn xs
<| function Cons (x, xs) ->
b.Add x
if b.Count < n
then bufferTime' n b timeOut xs clock
else consj b ^ bufferTime n timeOut xs
| Nil -> consj b nil
<| fun e -> consj b ^ error e
<|> clock ^=> fun _ ->
consj b ^ bufferTime n timeOut xs
:> Job<_>
and bufferTime n timeOut xs =
if n < 1 then failwith "bufferTime: n < 1" else
Job.tryIn xs
<| function Cons (x, xs) ->
let b = ResizeArray<_> (1)
b.Add x
if n <> 1
then bufferTime' n b timeOut xs ^ memo timeOut
else consj b ^ bufferTime n timeOut xs
| Nil -> nilj
<| error
|> memo

let values (xs: Stream<'x>) : Alt<'x> =
let vs = Ch ()
let (inc, dec) =
Expand Down
14 changes: 14 additions & 0 deletions Libs/Hopac/Stream.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,20 @@ module Stream =
/// at most given number of elements.
val buffer: int -> Stream<'x> -> Stream<array<'x>>

/// Converts a stream of elements into a stream of non-overlapping buffers of
/// at most given number of elements obtained within the given time limit. To
/// avoid busy-wait polling, the time limit is started when the first element
/// for a new buffer is obtained.
#if DOC
///
/// Here is an example diagram with `bufferTime 3`:
///
///> input: 1 234 56
///> timeout: +-----x +-! +-----x
///> output: [1] [234] [56]
#endif
val bufferTime: int -> timeout: Job<_> -> Stream<'x> -> Stream<ResizeArray<'x>>

//# Accumulating state

/// Returns a stream whose elements are computed using the given job and
Expand Down