foreach loop
ixid via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Nov 4 03:25:34 PST 2015
On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:
>>Can you help me out please. Thx.
reduce!((x, y) => x + !y)(0, arr).writeln;
This would probably be the preferred way, that uses a lambda
function (x, y) => x + !y which adds the inverse of the next
array value (y) to the total so far (x). You have to provide 0 as
the first argument to reduce as it is the seed, otherwise it will
use the first value in the array as the seed and convert it to an
int, making the total 1 too high as the first value is 'true'.
You can also use a string but this is frowned on style-wise
though in this case it is clearer:
reduce!"a + !b"(0, arr).writeln;
More information about the Digitalmars-d-learn
mailing list