scan

reduceは、レンジのすべての要素をたどりますが、scanはその途中結果を要素としてもつレンジです。

  1. auto scan(R r)
  2. auto scan(E iniValue, R r)
    scan
    (
    alias fun
    R
    E
    )
    ()
    if (
    isInputRange!R &&
    !isInfinite!R
    &&
    is(typeof(binaryFun!fun(iniValue, r.front)) : E)
    )

Examples

1 debug scope(failure) writefln("Unittest Failure :%s(%s) ", __FILE__, __LINE__);
2 debug scope(success) {writefln("Unittest Success :%s(%s)", __FILE__, __LINE__); stdout.flush();}
3 
4 // test cases : http://zvon.org/other/haskell/Outputprelude/scanl_f.html
5 assert(equal(scan!"a / b"(64, [4, 2, 4]), [64, 16, 8, 2]));
6 
7 int[] none;
8 assert(equal(scan!"a / b"(3, none), [3]));
9 
10 import std.algorithm : max;
11 assert(equal(scan!max(5, [1, 2, 3, 4]), [5, 5, 5, 5, 5]));
12 
13 assert(equal(scan!max(5, [1, 2, 3, 4, 5, 6, 7]), [5, 5, 5, 5, 5, 5, 6, 7]));
14 
15 assert(equal(scan!"2*a + b"(4, [1, 2, 3]), [4, 9, 20, 43]));

Meta