Last active
October 20, 2015 13:44
-
-
Save mickmaccallum/2cfefd3f610a703cb59d to your computer and use it in GitHub Desktop.
Swift 2 - FizzBuzz Generator
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
struct FizzBuzzGenerator: SequenceType { | |
private let bounds: Range<Int> | |
func generate() -> AnyGenerator<String> { | |
var current = bounds.startIndex | |
return anyGenerator { | |
guard current < self.bounds.endIndex else { | |
return nil | |
} | |
let output: String | |
switch (current % 3, current % 5) { | |
case (0, 0): | |
output = "FizzBuzz" | |
case (0, _): | |
output = "Fizz" | |
case (_, 0): | |
output = "Buzz" | |
default: | |
output = String(current) | |
} | |
current++ | |
return output | |
} | |
} | |
} | |
let fizzBuzzSequence = FizzBuzzGenerator(bounds: 1...100).lazy | |
for x in fizzBuzzSequence { | |
print(x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment