c4se記:さっちゃんですよ☆

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programming に分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡)

音樂は SoundCloud に公開中です。

考察は現在は主に Scrapbox で公表中です。

Programming は GitHub で開發中です。

Dartの並列処理 (Isolate) を使ってFizzBuzz

もうちょっとGoogle Dartを続けてみる。
DartのIsolateを意味もなく使ってFizzBuzzをする。browser applicationではなく、console applicationとして作る。読んで迷わないであろうところでは、型annotationを省略した。

#import('dart:isolate');

void main() {
  SendPort sendPort = spawnFunction(fizzbuzz);
  Future<String> fbStr = sendPort.call(100);
  fbStr.then((value) => print(value));
}

void fizzbuzz() {
  port.receive((int n, SendPort sendPort) {
    List<String> fbResult = [];
    for (int i = 1; i <= n; ++i) {
      if (i % 15 == 0) fbResult.add('FizzBuzz');
      else if (i % 3 == 0) fbResult.add('Fizz');
      else if (i % 5 == 0) fbResult.add('Buzz');
      else fbResult.add('$i');
    }
    String fbStr = '';
    fbResult.forEach((elm) => fbStr = fbStr.concat('$elm '));
    sendPort.send(fbStr);
  });
}

どうもentry pointとして呼ばれるvoid main()自体が、main isolateとして呼ばれているらしい。IsolateとFutureの非同期messagingが基礎的な実行model、きっと。