LoginSignup
0

More than 5 years have passed since last update.

複数の Channelからデータを受け取る

Last updated at Posted at 2015-11-26

追記: earlist構文は 2015年 12月 5日付で削除されました. 代わりに react + wheneverを使う必要があるようです.

この機能は 2015年 11月 26日現在バグだらけです. CPUを喰いまくる, lexical scopeがおかしくなるといった問題があります. ご利用の際はご注意ください.

Goで言うところの selectです.
Perl6では earliestを使います. 以下に例を示します.

use v6;

my $c = Channel.new;
my $e = Channel.new;

start {
    for 1..10 {
        $c.send($_);
        sleep 0.5;

        $e.send("Error!!!!!") if $_ == 7;
    }
    $c.close;
}

loop {
    # 待ち受けるチャネルを指定. *(ワイルドカードも可能)
    earliest $c, $e {
        # moreで sendされたメッセージを受け取る. ここも *が指定可能
        more $c -> $v { say "$v from channel" } 
        more $e -> $v { say $v; last; }
        # closeした場合に呼ばれるブロックは done. 1度しか呼ばれないわけではない.
        done $c -> { last; }
    }
}

say "Done";

結果は以下のようになります.

% perl6 test.pl6
1 from channel
2 from channel
3 from channel
4 from channel
5 from channel
6 from channel
7 from channel
Error!!!!!
Done

使用した Perl6

% perl6 --version
This is perl6 version 2015.11-68-gd3c278d built on MoarVM version 2015.11-5-g308b9b7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0