algorithm's .filter!() by range key

Charles via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 9 12:48:08 PST 2016


On Tuesday, 9 February 2016 at 20:44:34 UTC, cym13 wrote:
> On Tuesday, 9 February 2016 at 20:40:44 UTC, Charles wrote:
>> This seems to be true of any range function really... is there 
>> a way to access the key within my range?
>>
>> Example of what I want to do:
>>
>> auto x = [1,2,3,4,5];
>> x.filter( x_key % 2 == 1 ).sum(); // sum odd elements in array
>
> x.filter!(x_key => x_key % 2 == 1).sum();

Oh man, I really messed up my example, and did a poor one at that.

Better example:

auto x = [2,4,6,8,10];
x.filter( x_key => x_key % 2 == 1 ).sum(); // sums 2 + 6 + 10 == 
18


More information about the Digitalmars-d-learn mailing list