Created
November 13, 2014 07:41
-
-
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
This file contains hidden or 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 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