Usage
maprange(x, window, fn, do.pad=FALSE, by=1)Details
This function is intended to work primarily with time series-like
objects where the same statistic is computed over a rolling window
of the time series. In other packages this operation is referred to as
rollapply (e.g. zoo). This version has two significant differences from
other implementations: 1) it is purely functional, and therefore
easy to reason about; 2) it has consistent semantics with the
family of map functions; 3) it has an extra parameter by
to set the
gap between two contiguous windows. A typical use case for the by
parameter is like this: you have
a monthly time series, and need to calculate a metric over a rolling window
of 12 months, but the start point of each window is every quarter end.
Normally you'd have to roll through every months then filter out those
that start at quarter end. Now you can just set by=3
and get your
result in one line. Comparing the code for zoo:::rollapply.zoo, which is close to 100 lines,
versus the 3 lines separated into 2 function clauses clearly
demonstrates the conciseness inherent in functional programming.
Mathematics is known for being very compact and powerful. When
utilized appropriately, functional programs share this same property.