Seed Value for reduce function
Ali Çehreli
acehreli at yahoo.com
Sun Dec 10 06:01:49 UTC 2017
On 12/09/2017 06:19 PM, Vino wrote:
> Tried with fold even then it is failing same as reduce with the same
> error when there is empty folder
>
> auto SdFiles = Array!ulong(dirEntries(d, SpanMode.depth).map!(a =>
> a.size).fold!((a,b) => a + b))[].filter!(a => a > Size);
When no seed value is specified, fold (and reduce) take the first
element as the seed. When the range is empty, then there is a run-time
failure:
object.Exception@/usr/include/dmd/phobos/std/algorithm/iteration.d(2794):
Cannot reduce an empty input range w/o an explicit seed value.
The solution is to provide an explicit seed value:
import std.container;
import std.file;
import std.algorithm;
void main() {
const d = "/tmp/empty_folder";
const Size = 42;
auto SdFiles = Array!ulong(dirEntries(d, SpanMode.depth).map!(a =>
a.size).fold!((a,b) => a + b)(size_t(0)))[].filter!(a => a > Size);
}
I chose size_t(0) but you can use a variable, size_t.init, etc.
Ali
More information about the Digitalmars-d-learn
mailing list