Skip to content

Instantly share code, notes, and snippets.

@obecker
Last active February 8, 2017 03:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save obecker/3dc99571adeead7d25ff to your computer and use it in GitHub Desktop.
Save obecker/3dc99571adeead7d25ff to your computer and use it in GitHub Desktop.
FizzBuzz implementation in Swift
let numbers = AnySequence { () -> AnyGenerator<Int> in
var i = 1
return anyGenerator {
return i++
}
}.lazy
let fizzes = numbers.map { $0 % 3 == 0 ? "Fizz" : "" }
let buzzes = numbers.map { $0 % 5 == 0 ? "Buzz" : "" }
let pattern = zip(fizzes, buzzes).lazy.map { (fizz, buzz) in fizz + buzz }
let fizzbuzz = zip(pattern, numbers).lazy.map { (p, n) in p.isEmpty ? String(n) : p }
for fb in fizzbuzz.prefix(31) {
print(fb)
}
@obecker
Copy link
Author

obecker commented Nov 26, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment