Skip to content

Instantly share code, notes, and snippets.

@rizumita
Created March 19, 2016 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rizumita/74ad052c4db2b7a68100 to your computer and use it in GitHub Desktop.
Save rizumita/74ad052c4db2b7a68100 to your computer and use it in GitHub Desktop.
ズンドコキヨシをSwiftのAnyGeneratorとlazyで ref: http://qiita.com/rizumita/items/07640c4adf208d52b91d
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment