Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl6 General approach to modifying an array while looping through it

Tags:

arrays

loops

raku

Is it safe in Perl6 (as opposed to perl5 or other languages) to loop through an array while modifying it? For instance, if I have an array of websites to download, and I add failed downloads to the end of the array to re-download, will perl6 behave as expected? (I have about 50k links to download and trying to test all out would be time consuming.)

If not safe, what is a general approach? I have been thinking of storing links of interrupted downloads in another array, and loop through that array after original array was done. However, this is like a fox chasing its tail because I have to store failed downloads in another array (or overwrite the original array).

like image 292
lisprogtor Avatar asked Dec 19 '18 17:12

lisprogtor


People also ask

How to go through an array with for loop?

For Loop to Traverse Arrays. We can use iteration with a for loop to visit each element of an array. This is called traversing the array. Just start the index at 0 and loop while the index is less than the length of the array.

What property of the array can be used when looping?

If we want to loop through an array, we can use the length property to specify that the loop should continue until we reach the last element of our array. In the loop above, we first initialized the index number so that it begins with 0 .


1 Answers

It is definitely safe in a single-threaded environment:

my @a = ^5;
for @a { 
    @a.push: $_ + 10 if $_ < 30
}
say @a

[1 2 3 4 11 12 13 14 21 22 23 24 31 32 33 34]

In a multi-threaded environment (which is what is better be used in your task) nothing can be taken for granted. Therefore appending of a new element to the array is better be wrapped into a Lock:

my @a = ^5;
my Lock $l .= new;
for @a {
    start {
        ... # Do your work here 
        $l.protect: {
            @a.push: $_ with $site
        }
    }
}
say @a

Note that the last sample won't work as expected because all started threads must be awaited somewhere within the loop. Consider it just as a basic demo.

Yet, locking is usually be avoided whenever possible. A better solution would use Channel and react/whenever blocks.

like image 183
Vadim Belman Avatar answered Oct 19 '22 00:10

Vadim Belman