Last active
February 8, 2017 03:50
-
-
Save obecker/3dc99571adeead7d25ff to your computer and use it in GitHub Desktop.
FizzBuzz implementation in Swift
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
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also http://www.obqo.de/blog/2015/11/25/true-lazy-sequences/