Inline below is some code that uses this idea along with a "scan" (similar to a fold) to accomplish the moving average.