Update
In version 10.1 the overhang problem described below has been removed. (In the default form; with Method -> "Legacy"
the behavior below returns.) I am therefore tagging this as a bug.
Beware that MovingMap
syntax has incompatibly changed in 10.1 as well.
This appears to be a bug in MovingMap
. The function is applied beyond the left edge of the list and then the result is discarded:
echo[x_] := (Print[x]; x)
MovingMap[echo, Range@5, {2}]
{0,1}
{1,2}
{2,3}
{3,4}
{4,5}
{{1, 2}, {2, 3}, {3, 4}, {4, 5}}
I can see no reason to use the padding element 0
here. Interestingly when one specifies a different padding element that first result is not discarded:
MovingMap[Identity, Range@5, {2}, foo]
{{foo, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}}
The documentation for the padding parameter is:
So supposedly the default is no padding, but that is not true; the default padding is 0
, and then the result is discarded. Again:
MovingMap[echo, Range@5, {5}]
{0,0,0,0,1}
{0,0,0,1,2}
{0,0,1,2,3}
{0,1,2,3,4}
{1,2,3,4,5}
{{1, 2, 3, 4, 5}}
There is no reason for the function (echo
) to be applied the first four times, and in fact this directly contradicts the documentation which states that "no padding" is the default.
Also weird is this case:
MovingMap[echo, Range@5, {5, 2}]
{0,0,0,0,1}
{0,0,0,1,3}
{0,0,1,3,5}
MovingMap[echo, {1, 2, 3, 4, 5}, {5, 2}]
So even though it appears that the parameters are invalid and the entire MovingMap
expression is returned unevaluated, the echo
function is still applied to three times.