How do I zip an iterator in reverse order? - Chapel

How can I zip iterate in reverse order? I need to transfer the elements of an auxiliary array.

My code looks like this:

for (x,y) in zip({c..d by stride},{a..b by stride},){
    A1[x]=A1[y];
}

      

I need to do this in reverse order (i.e. b -> a and d -> c) to avoid being overwritten in the case of an overlapping area. (a..b is always before c..d).

+3


source to share


1 answer


Several things to point out.

First, in your example code, it uses

{c..d by stride}

      

eg. {} Create a domain variable, but you just want to iterate over it. You can iterate over a range directly, and it is syntactically easier and faster. That is, do not write

for i in {1..10} { ... } // unncessary domain

      

write this

for i in 1..10 { ... } // good

      

Now, to your question. The range is iterated backward in negative steps. Like this:



for i in 1..5 by -1 {
  writeln(i);
}

      

outputs

5
4
3
2
1

      

Such reverse iteration can be zipped, for example:

for (i,j) in zip( 1..5 by -1, 10..50 by -10 ) {
  writeln( (i,j) );
}

      

outputs

(5, 50)
(4, 40)
(3, 30)
(2, 20)
(1, 10)

      

+4


source







All Articles