4
4

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.

ReactiveCocoaでズンドコキヨシ

Last updated at Posted at 2016-03-30

RxSwiftでのズンドコキヨシはあるようですがReactiveCocoaでのズンドコキヨシがなかったので作ってみました。

enum Zundoko: String {
    case Zun = "ズン"
    case Doko = "ドコ"
    
    static func next() -> Zundoko {
        return arc4random_uniform(2) % 2 == 0 ? .Zun : .Doko
    }
}

SignalProducer<Zundoko, NoError> {
    observer, disposable in
    disposable += QueueScheduler().scheduleAfter(NSDate(), repeatingEvery: 0.1) {
        observer.sendNext(Zundoko.next())
    }
    }.scan([Zundoko]()) { (array, value) -> [Zundoko] in
        print(value.rawValue)
        return Array((array + [value]).suffix(5))
    }.takeWhile { array -> Bool in
        array != [.Zun, .Zun, .Zun, .Zun, .Doko]
    }.startWithCompleted {
        print("キ・ヨ・シ!")
}

SignalProducerでZunもしくはDokoを連続して作成します。scanで最終5個までの配列を作成します。takeWhileで終了条件であるズンズンズンズンドコと照合して終了するかを決定します。終了条件に合致するとキ・ヨ・シ!を出力して終了です。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?