newbie to Streaming and Haskell here.
I've been playing around with the streaming library and I'm particularly interested in understanding the chunks part. Eg:
S.print $ S.delay 1.0 $ concats $ chunksOf 2 $ S.each [1..10]
Or:
S.print $ concats $ S.maps (S.delay 1.0) $ chunksOf 2 $ S.each [1..10]
Here I can introduce a delay after each element but what I want is to have a delay after each chunk, in this case a delay every second element. I tried this but doesn't compile:
S.print $ concats $ S.delay 1.0 $ chunksOf 2 $ S.each [1..10]
How can I achieve this?
What we need is a function that inserts a single delay at the end of a chunk stream, and pass that function to
maps.delaydoesn't work here because it put delays between each yielded value. But we can do it easily using functions fromApplicative:What is happening here?
mapsapplies a transformation to the "base functor" of theStream. In a "chunked stream" obtained withchunksOf, that base functor is itself aStream. Also, the transformation must preserve the return value of theStream.Streams can be sequenced with functions like(>>=) :: Stream f m a -> (a -> Stream f m b) -> Stream f m bif the next stream depends on the final result of the previous one, or with functions like(<*) :: Stream f m a -> Stream f m b -> Stream f m aif it doesn't.(<*)preserves the return value of the firstStream, which is what we want in this case.We do not want to yield any more elements, but only to introduce a delay effect, so we simply
liftIOthe effect into theStreammonad.Another way to insert delays after each yielded value of a
Streamis to zip it with an infinite list of delays: