-
Notifications
You must be signed in to change notification settings - Fork 30
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 slidingWindowSum #99
base: master
Are you sure you want to change the base?
Conversation
A couple concerns: My biggest concern is that for a number of purposes, the size of the window is quite important. If the stream proves to be smaller than the window, we need to know that. For example, if we're taking a moving average, and the stream comes up very short, then we'd naively end up with a total garbage result. Should we have variants that add a return value indicating something about this? Another concern: it seems rather useful to use this with newtypes from |
Oh, one more thing: the name kind of sucks. Should it be |
84d7e83
to
8ec3763
Compare
Yet another question: do we want a |
The CI failure is some GHC HEAD nonsense. |
@andrewthad I know you've stepped away from this package, but I'd very much appreciate your thoughts on this one. |
a3c5a40
to
0bffbf9
Compare
`slidingWindow` typically helps write only functions with complexity around `O(n*k)`, where `n` is the number of elements in the stream and `k` is the size of the window. In many cases, this can be reduced to `O(n)` by looking not at the window itself but instead the sum of that window in some `Semigroup`. This can be used, for example, to implement moving averages such as arithmetic, geometric, or harmonic means.
The queues seem more useful than the sliding window function itself.
0bffbf9
to
6e8a5b1
Compare
Instead of a return value, perhaps members of the original stream up to the first full window could be re-yielded before starting with the windows themselves. Something like:
For cases in which there aren't enough elements to form a window, the isolated elements would be re-yielded but the stream with the window results would be empty. Also, this way we don't need to keep whole windows in memory in order to report "incomplete" windows. |
Pardon my ignorance in this area, but here are the things that confused me. This internal data structure looks a lot like a finger tree, but it differs from Patterson's finger tree:
But I guess that's not what it's actually based on. The documentation says that this is "an implementation of Okasaki's implicit queues holding elements of some semigroup". It would be kind of nice to have a link to a reference implementation of Okasaki's implicit queues in Haskell so that someone could look at it and see how the data structure differs, but I don't know if there is a good library where this has already been done. After figuring out what was going on with the data structure, I think this seems fine though, and it's the ideal "purely functional" solution to the problem. It's a little sad that the |
@andrewthad, I really just mashed up Okasaki's structure with Hinze and Paterson's and checked that it was still okay. Yes, the structures are quite closely related, with Okasaki's simpler and less flexible. Which is faster for the purpose? Dunno. It does rather feel like it belongs in Some Other Library. semimeasure' :: a -> Maybe s
semimeasure'' :: r -> (s -> r) -> a -> r I just went with the bare minimum I needed here. @danidiaz, I really don't know enough about what people need to feel like I can evaluate whether your idea is the right approach. |
slidingWindow
typically helps write only functions withcomplexity around
O(n*k)
, wheren
is the number of elementsin the stream and
k
is the size of the window. In many cases,this can be reduced to
O(n)
by looking not at the window itselfbut instead the sum of that window in some
Semigroup
. This canbe used, for example, to implement moving averages such as
arithmetic, geometric, or harmonic means.