Skip to content

Instantly share code, notes, and snippets.

@kakajika
Last active April 10, 2016 07:49
Show Gist options
  • Select an option

  • Save kakajika/55244fe81395a62333fea653ed095b46 to your computer and use it in GitHub Desktop.

Select an option

Save kakajika/55244fe81395a62333fea653ed095b46 to your computer and use it in GitHub Desktop.
Swiftの再帰的enumとパターンマッチでズンドコ
import Foundation
enum Zundoko: CustomStringConvertible {
case Start
indirect case Zun(Zundoko)
indirect case Doko(Zundoko)
indirect case Kiyoshi(Zundoko)
var description: String {
switch self {
case Start: return "スタート!"
case Zun(let zd): return "\(zd)\nズン"
case Doko(let zd): return "\(zd)\nドコ"
case Kiyoshi(let zd): return "\(zd)\nキ・ヨ・シ!"
}
}
func zundoko() -> Zundoko {
if case .Doko(.Zun(.Zun(.Zun(.Zun)))) = self {
return .Kiyoshi(self)
}
return (arc4random_uniform(2) > 0) ? Zun(self).zundoko() : Doko(self).zundoko()
}
}
print(Zundoko.Start.zundoko())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment