Last active
August 23, 2020 20:26
-
-
Save shayelkin/5649844 to your computer and use it in GitHub Desktop.
Modulus-less fizzbuzz in Ruby
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
#!/usr/bin/env ruby | |
def fizzbuzz(n) | |
c = lambda {|i, s| ([nil] * i << s).cycle} | |
# Must first zip with a finite range, else we'd get infinite loop | |
Range.new(1, n).zip(c.call(2, 'Fizz'), c.call(4, 'Buzz')).map do |a,b,c| | |
s = "#{b}#{c}" | |
s.empty? ? a : s | |
end | |
end | |
fizzbuzz(100).each {|x| print "#{x}\n"} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This would have been nicer if I had a lazy zip function.