www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - filtering a row of a jagged array

reply DanielG <simpletangent gmail.com> writes:
int[][] whatever = [
     [0],
     [0, 1, 2],
     [5, 6, 7, 8, 9, 10]
];
writeln(whatever[2]);                    // [5, 6, 7, 8, 9, 10]
writeln(typeid(whatever[2]));            // int[]
auto x = whatever[2].filter(x => x > 7); // error

Error: template std.algorithm.iteration.filter cannot deduce 
function from argument types !()(int[], void), candidates are: ...

Online example:
https://run.dlang.io/is/LUXFuF

...

I'm guessing I need to give the compiler some help understanding 
that this is an array of ints, but 1) how, and 2) why? [if 
typeid() seems to understand just fine?]
Aug 11 2019
next sibling parent ag0aep6g <anonymous example.com> writes:
On 11.08.19 18:11, DanielG wrote:
 auto x = whatever[2].filter(x => x > 7); // error
You just forgot an exclamation mark here. auto x = whatever[2].filter!(x => x > 7); // works
Aug 11 2019
prev sibling parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Sunday, 11 August 2019 at 16:11:15 UTC, DanielG wrote:
 int[][] whatever = [
     [0],
     [0, 1, 2],
     [5, 6, 7, 8, 9, 10]
 ];
 writeln(whatever[2]);                    // [5, 6, 7, 8, 9, 10]
 writeln(typeid(whatever[2]));            // int[]
 auto x = whatever[2].filter(x => x > 7); // error

 Error: template std.algorithm.iteration.filter cannot deduce 
 function from argument types !()(int[], void), candidates are: 
 ...

 Online example:
 https://run.dlang.io/is/LUXFuF

 ...

 I'm guessing I need to give the compiler some help 
 understanding that this is an array of ints, but 1) how, and 2) 
 why? [if typeid() seems to understand just fine?]
You're missing an exclamation mark after filter - it takes the predicate as a template argument. This compiles just fine: auto x = whatever[2].filter!(x => x > 7); -- Simen
Aug 11 2019
parent reply DanielG <simpletangent gmail.com> writes:
Thank you both! Ugh, I have to roll my eyes at missing such a 
simple error. (This literally occurred in the context of a bunch 
of other code using 'map' and 'reduce' with the exclamation 
point, so I don't know why my brain turned off for 'filter')

Thanks again!
Aug 11 2019
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 08/11/2019 09:43 AM, DanielG wrote:
 such a simple 
 error
You're not alone. We want this bug fixed: https://issues.dlang.org/show_bug.cgi?id=17263 Ali
Aug 11 2019