I am pleased to have MovingMap in Mathematica 10. But, today, I encountered the problem that needs MovingMap with a periodic boundary condition.
To be specific, Consider the following example.
MovingMap[ f, Range[3], {2} ]
will gives the result {f[{1, 2}], f[{2, 3}], f[{3, 4}]}
.
However, what I want to get is {f[{1, 2}], f[{2, 3}], f[{3, 4}], f[{4,1}]}
.
One of the easiest and straightforward way would be calculating the last one and then join the lists together. But I want to know if there is more elegant way to do it. (I checked the optional arguments of MovingMap
but I couldn't find a solution.)
Map
withPartition
:f /@ Partition[Range[4], 2, 1, {1, 1}]
gives{f[{1, 2}], f[{2, 3}], f[{3, 4}], f[{4, 1}]}
$\endgroup$