windowedReduce

Generalized Windowed Reduce. * * See_Also: https://stackoverflow.com/questions/21004944/forward-difference-algorithm * See_Also: http://forum.dlang.org/thread/ujouqtqeehkegmtaxebg@forum.dlang.org#post-lczzsypupcfigttghkwx:40forum.dlang.org * See_Also: http://rosettacode.org/wiki/Forward_difference#D

ref
windowedReduce
(
Reduction reduction = Reduction.forwardDifference
R
)
()
if (
isInputRange!R
)

Examples

import std.datetime : Clock, SysTime, Duration;
import std.algorithm.iteration: map;
SysTime[] times;
immutable n = 4;
foreach (i; 0..n)
    times ~= Clock.currTime;
version(print) dbg(times);
auto spans = times.windowedReduce!(Reduction.forwardDifference);
version(print) dbg(spans);
// dbg(*(cast(ulong*)&(spans.front)));
version(print) dbg(Duration.sizeof);
immutable i = [1, 4, 9, 17];
assert(i.windowedReduce!(Reduction.forwardDifference).equal ([+3, +5, +8]));
assert(i.windowedReduce!(Reduction.backwardDifference).equal([-3, -5, -8]));
assert(i.windowedReduce!(Reduction.sum).equal ([+5, +13, +26]));
assert([1].windowedReduce.empty);
version(print) dbg(i.windowedReduce!(Reduction.sum));

Meta