Skip to content

Instantly share code, notes, and snippets.

@mpurland
Created November 13, 2014 07:41
Show Gist options
  • Select an option

  • Save mpurland/c704e67f9ceb2e00260b to your computer and use it in GitHub Desktop.

Select an option

Save mpurland/c704e67f9ceb2e00260b to your computer and use it in GitHub Desktop.
Swift: Generic method for enumerating all values of an enum of type Int
enum Reindeer: Int {
case Dasher, Dancer, Prancer, Vixen, Comet, Cupid, Donner, Blitzen, Rudolph
static var allValues: [Reindeer] {
return allValuesGenerator({ Reindeer(rawValue: $0) })
}
}
func allValuesGenerator<T>(fromValue: Int -> T?) -> [T] {
return Array(
SequenceOf {
() -> GeneratorOf<T> in
var i = 0
return GeneratorOf<T> {
return fromValue(i++)
}
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment