Launching Self-Service Channels in Perl 6

I would like to configure several threads working simultaneously on the channel, and each of these flows should also feed the channel. One of the threads will decide when to stop. However, this is the closest I have done for this:

use Algorithm::Evolutionary::Simple;

my $length = 32;
my $supplier = Supplier.new;
my $supply   = $supplier.Supply;
my $channel-one = $supply.Channel;
my $pairs-supply = $supply.batch( elems => 2 );
my $channel-two = $pairs-supply.Channel;

my $single = start {
    react  {
        whenever $channel-one -> $item {
            say "via Channel 1:", max-ones($item);
        }
    }
}

my $pairs = start {
    react  {
        whenever $channel-two -> @pair {
        my @new-chromosome = crossover( @pair[0], @pair[1] );
        say "In Channel 2: ", @new-chromosome;
        $supplier.emit( @new-chromosome[0]);
        $supplier.emit( @new-chromosome[1]);
        }
    }
}

await (^10).map: -> $r {
    start {
    sleep $r/100.0;
        $supplier.emit( random-chromosome($length) );
    }
}

$supplier.done;

This stops after a series of outliers. And that probably doesn't work at the same time. I use channels instead of supplies and cranes because they do not start at the same time, but asynchronously. I need materials because I want to have a seudo channel that takes elements in pairs, as was done above; I have not seen a way to do this with clean channels. There is no difference above if I changed the source emitto the channel send.

So a few questions here

  • react ? , ?

  • , , $pairs ?

  • "" , ?

1: $supplier.done , . whenever, , .

+4
1

,

my Channel $c .= new;
my Channel $c2 = $c.Supply.batch( elems => 2).Channel;
my Channel $output .= new;
my $count = 0;
$c.send(1) for ^2;

my $more-work = start react whenever $c2 -> @item {
    if ( $count++ < 32 ) {
        $c.send( @item[1]);
    my $sum = sum @item;
    $c.send( $sum );
    $output.send( $sum ); 
    } else {
    $c.close;
    }

}
await $more-work;
loop {
    if my $item = $output.poll {
    $item.say
    } else {
    $output.close;
    }
    if $output.closed  { last };
}

, , ($c.Supply), (batch( elems => 2)) , . , , ( , ) , . , , twos, . , ; , . :

  • , , .
  • . , , .
  • , , . , , .
+1

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


All Articles