Skip to content

Instantly share code, notes, and snippets.

@shayelkin
Last active August 23, 2020 20:26
Show Gist options
  • Save shayelkin/5649844 to your computer and use it in GitHub Desktop.
Save shayelkin/5649844 to your computer and use it in GitHub Desktop.
Modulus-less fizzbuzz in Ruby
#!/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"}
@shayelkin
Copy link
Author

This would have been nicer if I had a lazy zip function.

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