Created
March 19, 2016 13:02
-
-
Save rizumita/74ad052c4db2b7a68100 to your computer and use it in GitHub Desktop.
ズンドコキヨシをSwiftのAnyGeneratorとlazyで ref: http://qiita.com/rizumita/items/07640c4adf208d52b91d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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