Seed Value for reduce function

Vino vino.bheeman at hotmail.com
Sun Dec 10 00:49:31 UTC 2017


On Sunday, 10 December 2017 at 00:39:07 UTC, Vino wrote:
> Hi All,
>
>   Request you help on how to add the seed value for the reduce 
> function, below is the scnerio
>
> Program 1 : Works
>
> import std.algorithm;
> void main () {
> int[] ara = [1,2 ,3];
> auto sum1 = ara.reduce!((a,b) => a + b);
> writeln(sum1);
> }
>
> Program 2: Works
> void main () {
> int[] arrb = [];
> auto sum1 = reduce!((a,b) => a + b)(0 , arb);
> writeln(sum1);
> }
>
> So how to add seed value for the below code as same as program 
> 1 without calling the seed value and array at the end rather 
> than calling it as arc.reduce!((a,b) => a + b);
>
> void main () {
> int[] arc = [];
> auto sum1 = arc.reduce!((a,b) => a + b);
> writeln(sum1);
> }
>
> From,
> Vino.B

Another example: The below code does errors out with the below 
error in there are any empty folders, else it works fine.

import std.stdio;
import std.file;
import std.container;
import std.algorithm;
ulong Size = 10;

auto SdFiles = Array!ulong(dirEntries("C:\\Temp\\BACKUP", 
SpanMode.depth).map!(a => a.size).reduce!((a,b) => a + 
b))[].filter!(a => a  > Size);

Error:
object.Exception at C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(2794): Cannot reduce an empty input range w/o an explicit seed value.

From,
Vino.B


More information about the Digitalmars-d-learn mailing list