Simple Tasks in Streamly

Here is another (simple?) task I am struggling to solve with Streamly.

I want to write a function like this:

slidingWindowFuture :: Float -> Stream m (Float, a) -> Stream m (Float, Seq a)
slidingWindowFuture wlen input = undefined

Let’s say that the input stream is (t[0], v[0]), (t[1], v[1]), ....

Then, the output stream should be (t[0], s[0]), (t[1], s[1]), ... where s[i] = Seq.fromList [v[i], v[i + 1], ... v[n] such that n has the following property:

  • t[n+1] should be the smallest t[j] such that t[j] - t[i] > wlen

If such an n is not defined, then neither is the pair (t[i], s[i])

For example, say

  • wlen is 2
  • The input stream is Stream.fromList [(0, 'a'), (1, 'b'), (2, 'c'), (2.5, 'd'), (3, 'e'), (3.1, 'f')]
  • Then, the output stream is [(0, Seq.fromList ['a', 'b', 'c']), (1, Seq.fromList ['b', 'c', 'd', 'e'])]
  • Note that the output tuple with (2, ...) is not yet defined because the stream transformer is still waiting for an element with time more than 4.