www.digitalmars.com         C & C++   DMDScript  

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

reply 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.

I have read about slicing, but it can only subset contiguous 
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
parent reply 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.

 I have read about slicing, but it can only subset contiguous 
 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
next sibling parent reply 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
parent 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
prev sibling parent reply 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
parent 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