How to make a built-in IF array constructor in a Chapel?

I would like to create a subset of the domain based on the conditional. I could do a loop, but I'm looking to see if I can use the built-in if.

Just re-creating the array dlooks like

var d = {1..8};
var e =  [0.875, 0.625, 0.625, 1.0, 0.625, 0.875, 0.625, 0.625];
var p = 0.7;

var vs = for i in d do i;
writeln(" vs: ", vs);

However, I want to extract dwhere e[d] < pin vs. Is there such an approach?

vs = [i in d where e[i] < p]
writeln(vs);  // {2,3,5,7,8}
+4
source share
1 answer

This will give you the desired result:

var vs = for i in d do
           if e[i] < p then i;

Note that vsthis is an array, not a domain. If you want to use a domain that you can use, you must use an associative domain:

var vs : domain(int) = for i in d do
                         if e[i] < p then i;

This example will turn into something like this:

var vs : domain(int);
for i in d {
  if e[i] < p then
    vs.add(i);
}
+3
source

Source: https://habr.com/ru/post/1687373/


All Articles