www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - algorithm's .filter!() by range key

reply Charles <csmith.ku2013 gmail.com> writes:
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
Feb 09 2016
next sibling parent reply cym13 <cpicard openmailbox.org> writes:
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();
Feb 09 2016
parent Charles <csmith.ku2013 gmail.com> writes:
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
Feb 09 2016
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/9/16 3:40 PM, 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
An array is not an indexed range. It only works with foreach by key because of special foreach behavior. What you want is std.range.enumerate: import std.range; import std.algorithm; import std.stdio; void main() { auto x = [1, 2, 3, 4, 5]; writeln(x.enumerate.filter!(a => a[0] % 2 == 1).map!(a => a[1]).sum); // 6 } -Steve
Feb 09 2016
parent reply Charles <csmith.ku2013 gmail.com> writes:
On Tuesday, 9 February 2016 at 20:48:01 UTC, Steven Schveighoffer 
wrote:
 On 2/9/16 3:40 PM, 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
An array is not an indexed range. It only works with foreach by key because of special foreach behavior. What you want is std.range.enumerate
Exactly! Thanks! Interestingly, hackerrank doesn't seem to have it. They're using 2.067.0-b1 on Ubuntu 14.04.
Feb 09 2016
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/09/2016 12:54 PM, Charles wrote:
 On Tuesday, 9 February 2016 at 20:48:01 UTC, Steven Schveighoffer wrote:
 On 2/9/16 3:40 PM, 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
An array is not an indexed range. It only works with foreach by key because of special foreach behavior. What you want is std.range.enumerate
Exactly! Thanks! Interestingly, hackerrank doesn't seem to have it. They're using 2.067.0-b1 on Ubuntu 14.04.
For this specific problem, you can combine drop() and stride() (and even sum()! ;) ): import std.range; import std.algorithm; import std.stdio; void main() { auto x = [1, 2, 3, 4, 5]; writeln(x.drop(1).stride(2).sum); // 6 } Ali
Feb 09 2016