8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C# と Reactive Extensions でズンドコキヨシ

Last updated at Posted at 2016-03-12

流行り?に乗っていくスタイル。

Zondoko.cs
var random = new Random();
var K = "キ・ヨ・シ!";
var PATTERN = new string[] { "ずん", "ずん", "ずん", "ずん", "どこ" };

Observable.Interval(TimeSpan.FromMilliseconds(100))
    .Select(_ => (random.Next() % 2 == 0) ? "ずん" : "どこ") // ランダムに ずんorどこ
    .Scan(new List<string>(), (queue, x) => // 最大5つのQueueに貯める
        {
            queue.Add(x);
            while (queue.Count > PATTERN.Count) { queue.RemoveAt(0);}
            return queue;
        })
    .SelectMany(queue => queue.SequenceEqual(PATTERN) ? // パターンと一致したら…
        Observable.Concat(
            Observable.Return(queue.Last()),   // Queueの最後
            Observable.Return(K),              // + キ・ヨ・シ!
            Observable.Return(string.Empty)) : // + 空文字(終了判定用)
        Observable.Return(queue.Last()))
    .TakeWhile(x => !string.IsNullOrEmpty(x))  // 空文字になるまで繰り返す
    .Subscribe(
        x => Console.WriteLine(x),
        () => Console.WriteLine("complete!!"));

どこ
どこ
ずん
ずん
ずん
どこ
どこ
どこ
ずん
どこ
ずん
ずん
ずん
ずん
どこ
キ・ヨ・シ!
complete!!

SelectMany に頼ってるのが気に入らない。。。

8
8
0

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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?