7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

怠惰な FizzBuzz を作ってみた

Last updated at Posted at 2017-11-16

怠惰な FizzBuzz

お風呂に入ろうとしていたら、突然思いついたのですかさず試してメモしました。

def genarate_lazy_list(number, word)
  loop.lazy.flat_map { [*Array.new(number - 1, nil), word] }
end

numbers = 1.step.lazy
fizzes = genarate_lazy_list(3,'Fizz')
buzzes = genarate_lazy_list(5,'Buzz')
fizzbuzzes = genarate_lazy_list(15,'FizzBuzz')

numbers
  .zip(fizzes, buzzes, fizzbuzzes)
  .map { |n, fizz, buzz, fizzbuzz| fizzbuzz || buzz || fizz || n }
  .take(30)
  .each { puts(_1) }
stdout
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz

fizzbuzzes が冗長だと思う場合は

require 'active_support'
require 'active_support/core_ext/object/blank'

numbers
  .zip(fizzes, buzzes)
  .map { |n, fizz, buzz| "#{fizz}#{buzz}".presence || n }
  .take(30)
  .each { puts(_1) }

宣言的な FizzBuzz

お風呂の中で思いついた。

# say(Fizz or Buzz or FizzBuzz).for_multiples_of(number) と書けるようにする。
def say(fizzbuzz)
  def fizzbuzz.for_multiples_of(number)
    loop.lazy.flat_map { [*Array.new(number - 1, nil), self] }
  end

  fizzbuzz
end

numbers = 1.step.lazy
fizzes = say('Fizz').for_multiples_of(3)
buzzes = say('Buzz').for_multiples_of(5)
fizzbuzzes = say('FizzBuzz').for_multiples_of(15)

numbers
  .zip(fizzes, buzzes, fizzbuzzes)
  .map { |n, fizz, buzz, fizzbuzz| fizzbuzz || buzz || fizz || n }
  .take(30)
  .each { puts(_1) }

おまけ

Python 版も実装しました。

:point_right: 怠惰な FizzBuzz を作ってみた (Python 版)

7
5
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?