Is logical subsetting of an array possible ?

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 14 04:01:20 PDT 2016


On Thursday, 14 July 2016 at 10:51:33 UTC, Nicholas Wilson wrote:
> On Thursday, 14 July 2016 at 10:43:12 UTC, Sahil wrote:

>> (I am totally new to D)
>
> easily. The function you are looking for is called 'filter'
>
> import std.algorithm;
> import std.array;
> int[] a = [ 4,5,8,1,3,2,9,10];
> auto b = a.filter!(e => e>3).array; //b contains the elements 
> 4,5,8,9,10

I should explain this a bit more if you are totally new to D.

although filter looks like a method for an array type it is not 
it is a function.
in D function like f(a,b) can be rewritten as a.f(b);

the '!' is the template instansiation operator (as filter is a 
template function) and its template parameter is the predicate to 
filter in this case ,(e => e>3).

As with anyone totally new to D i cannot recommend more highly 
Ali's book.




More information about the Digitalmars-d-learn mailing list