Created
August 10, 2017 15:47
-
-
Save JohnSundell/a5aa87784caafdbd4a3c33d2fdd79dcb to your computer and use it in GitHub Desktop.
A simple way to iterate over linear Int-based enums
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
struct EnumSequence<T: RawRepresentable> where T.RawValue == Int {} | |
extension EnumSequence: Sequence { | |
func makeIterator() -> AnyIterator<T> { | |
var rawValue = 0 | |
return AnyIterator { | |
let nextCase = T(rawValue: rawValue) | |
rawValue += 1 | |
return nextCase | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment