digitalmars.D.learn - Is logical subsetting of an array possible ?

• Sahil (12/12) Jul 14 2016 This is with reference to documentation about use of array in D
• Nicholas Wilson (7/19) Jul 14 2016 easily. The function you are looking for is called 'filter'
• Nicholas Wilson (10/18) Jul 14 2016 I should explain this a bit more if you are totally new to D.
• Sahil (3/14) Jul 14 2016 Yes, I have started with this book.
• Sahil (4/13) Jul 14 2016 Thanks for a prompt reply. This gives me confidence to explore D
• Steven Schveighoffer (6/19) Jul 14 2016 Please be aware, however, that filter is going to still iterate the
Sahil <sahilkakkar22 gmail.com> writes:
```This is with reference to documentation about use of array in D
(https://dlang.org/spec/arrays.html#array-setting).

In languages tailor-made for data mining, like R, a subset of an
array (not necessarily contiguous values) can be extracted as
shown below:

vec //a vector or array in R
c(1:length(vec))[vec > 3] //extract a new array having values of
vec that are greater than 3.

values. Correct me if I am wrong here.

How can the above functionality be achieved in D ?
(I am totally new to D)
```
Jul 14 2016
Nicholas Wilson <iamthewilsonator hotmail.com> writes:
```On Thursday, 14 July 2016 at 10:43:12 UTC, Sahil wrote:
This is with reference to documentation about use of array in D
(https://dlang.org/spec/arrays.html#array-setting).

In languages tailor-made for data mining, like R, a subset of
an array (not necessarily contiguous values) can be extracted
as shown below:

vec //a vector or array in R
c(1:length(vec))[vec > 3] //extract a new array having values
of vec that are greater than 3.

values. Correct me if I am wrong here.

How can the above functionality be achieved in D ?
(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
```
Jul 14 2016
Nicholas Wilson <iamthewilsonator hotmail.com> writes:
```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.
```
Jul 14 2016
Sahil <sahilkakkar22 gmail.com> writes:
```On Thursday, 14 July 2016 at 11:01:20 UTC, Nicholas Wilson wrote:
On Thursday, 14 July 2016 at 10:51:33 UTC, Nicholas Wilson
wrote:

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.

Yes, I have started with this book.
Thanks for the explanation!
```
Jul 14 2016
Sahil <sahilkakkar22 gmail.com> writes:
```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:
This is with reference to documentation about use of array in
D (https://dlang.org/spec/arrays.html#array-setting).

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

Thanks for a prompt reply. This gives me confidence to explore D
for faster implementations in data mining.
Thanks a lot :-)
```
Jul 14 2016
Steven Schveighoffer <schveiguy yahoo.com> writes:
```On 7/14/16 7:03 AM, Sahil wrote:
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:
This is with reference to documentation about use of array in D
(https://dlang.org/spec/arrays.html#array-setting).

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

Thanks for a prompt reply. This gives me confidence to explore D for
faster implementations in data mining.
Thanks a lot :-)

Please be aware, however, that filter is going to still iterate the
array. So the subset operation may not be as performant as a subset
operation and data type that is written to deal with these kinds of
operations.

-Steve
```
Jul 14 2016