Purely Functional Sliding Window Aggregation Algorithm
byorgey.github.io
byorgey.github.io
I also found an interesting streaming version here recently: https://signalsmith-audio.co.uk/writing/2022/constant-time-p...
EDIT: On closer inspection, this method is equivalent to the one I described, and not the one I'm used to seeing with queues (that starts my tutorial). The stack-reversing step is what forms a backwards scan. The combination of turning it sequential by taking in one element at a time but then expressing this in functional programming makes for a complicated presentation, I think.
The whole point of the post is that this is easy to implement for sum, but is difficult for max. Posting how someone solves the problem for sum isn't really addressing anything new here.
I've seen it in tech interviews for years.
Given an n length array of integers, and an integer k, output the max value for each k sized contiguous subarray.
sum is much easier than max.
But the article even discusses a generic solution for any monoid.
Either sum or max is easier than sum and max and a bunch of other operations all at the same time.
https://gist.github.com/unrealwill/5ca4db9beefafaa212465277b...
So is everything. BTW, read the article. No monads in there, unless you believe monoids are monads.