DEV Community

Discussion on: Lazy FizzBuzz

Collapse
 
hanachin profile image
Seiei Miyagi • Edited

Awesome! zipping infinite generators is really cool๐Ÿ‘

Tips: Array#cycle can be used to generate infinite sequence of Fizz and Buzz.

numbers = 1.step
fizzes = [nil, nil, "Fizz"].cycle
buzzes = [nil, nil, nil, nil, "Buzz"].cycle

numbers \
  .lazy \
  .zip(fizzes, buzzes) \
  .map { |n, *yells| yells.any? ? yells.join : n } \
  .take(30) \
  .each(&method(:puts))
Collapse
 
quanon profile image
QUANON

Thank you! The method makes it simpler ๐Ÿ’ช