The trick is to combine `pipes` with the `foldl` library. In fact, given any `Fold`, you can convert it into a scanning pipe like this:

    convert :: Monad m => Fold a b -> Pipe a b m r
    convert = Control.Foldl.purely Pipes.Prelude.scan

So now you have a smaller problem: write a fold for a moving average of a stream of numbers. It turns out that there is a nice way to do this in O(1) space if you do an exponential moving average:

http://lpaste.net/100765

You can also do an ordinary moving average, too, but that requires O(N) space (where N is the window size). It basically involves storing the last N elements as the fold's internal state.

The `foldl` library is easy to learn. Just read the module header here and the documentation for the `Fold` type:

http://hackage.haskell.org/package/foldl-1.0.2/docs/Control-Foldl.html

Now, technically, you could do all of this without using the `foldl` library at all, just by passing the folding logic directly to `Pipes.Prelude.scan`, but by using `foldl` you make it really easy to add additional metrics to your `Fold` while still passing over the data just once.

On 3/5/2014 8:01 AM, Alex Rozenshteyn wrote:
I feel like I must be missing something rather basic, but I have been trying to figure out how to use pipes to compute a moving average of a stream of numbers. I've written code to do the EWMA, but I can't figure out how to do the sliding window. It doesn't help that I'm a bit of a pipes beginner.

Advice is welcome and appreciated.

Thank you.
--
You received this message because you are subscribed to the Google Groups "Haskell Pipes" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected] <mailto:[email protected]>. To post to this group, send email to [email protected] <mailto:[email protected]>.

--
You received this message because you are subscribed to the Google Groups "Haskell 
Pipes" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].

Reply via email to