foreach loop

Edwin van Leeuwen via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 3 09:09:29 PST 2015


On Tuesday, 3 November 2015 at 16:55:44 UTC, wobbles wrote:
> On Tuesday, 3 November 2015 at 15:42:16 UTC, Edwin van Leeuwen 
> wrote:
>> On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote:
>>>> writefln("Count is: %s", arr
>>>>   .filter!(a => a==true)
>>>>   .sum);
>>>>
>>>> // Note: std.algorithm.sum is the same as
>>>> // std.algorithm.reduce!((a,b)=a+b);
>>>
>>
>> Shouldn't you be using walkLength instead of sum, since you 
>> are counting the left over values?
>>
>> import std.range : walkLength;
>> writefln("Count is: %s", arr
>>   .filter!(a => a==false)
>>   .walkLength);
>
> That would work also yes.
> Be interesting to know which is more efficient actually - I 
> suspect they're very similar.

false converts to zero, so
[false,false,false].sum == 0

Of course true converts to one ->
[true,true,true].sum == 3


More information about the Digitalmars-d-learn mailing list