3
3

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.

ズンドコキヨシをSwiftのAnyGeneratorとlazyで

Posted at

小学生の息子がズンドコキヨシをPythonで作ったので、もうかなり出遅れたけど私はSwiftのAnyGeneratorとlazyを使ってやってみた。

enum Zundoko: String {
    case Zun = "ズン"
    case Doko = "ドコ"
}

var zundoko = Array<Zundoko>()
let gen = anyGenerator { return arc4random_uniform(2) == 0 ? Zundoko.Zun : Zundoko.Doko }
    .lazy
    .map { (zd) -> [Zundoko] in
        zundoko.append(zd)
        if zundoko.count > 5 {
            zundoko.removeFirst()
        }
        return Array(zundoko)
    }
    .filter { (zundoko: [Zundoko]) -> Bool in
        print(zundoko.last!.rawValue)
        let result = zundoko.map { $0.rawValue } .joinWithSeparator("") == "ズンズンズンズンドコ"
        if result { print("キ・ヨ・シ!") }
        return result
}

gen.prefix(1).generate().next()
3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?