Skip to content

Instantly share code, notes, and snippets.

@mickmaccallum
Last active October 20, 2015 13:44
Show Gist options
  • Save mickmaccallum/2cfefd3f610a703cb59d to your computer and use it in GitHub Desktop.
Save mickmaccallum/2cfefd3f610a703cb59d to your computer and use it in GitHub Desktop.
Swift 2 - FizzBuzz Generator
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