Created
November 18, 2012 03:43
-
-
Save supaspoida/4103372 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
mod3 = ->(i) { (i % 3).zero? } | |
mod5 = ->(i) { (i % 5).zero? } | |
both = ->(i) { mod3[i] && mod5[i] } | |
rules = { | |
'FizzBuzz' => ->(i) { both[i] }, | |
'Fizz' => ->(i) { mod3[i] }, | |
'Buzz' => ->(i) { mod5[i] } | |
} | |
fizzbuzz = ->(i) { | |
val, match = rules.detect { |val,rule| rule[i] } | |
match ? val : i | |
} | |
puts (1..35).map &fizzbuzz |
This file contains hidden or 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
# Usage: FizzBuzz[100] | |
module FizzBuzz | |
extend self | |
MOD3 = ->(i) { (i % 3).zero? } | |
MOD5 = ->(i) { (i % 5).zero? } | |
BOTH = ->(i) { MOD3[i] && MOD5[i] } | |
def [](count) | |
(1..count).map &fizzbuzz | |
end | |
def fizzbuzz | |
->(i) { | |
val, match = rules.detect { |val,rule| rule[i] } | |
match ? val : i | |
} | |
end | |
def rules | |
{ | |
'Fizz' => ->(i) { MOD3[i] }, | |
'Buzz' => ->(i) { MOD5[i] }, | |
'FizzBuzz' => ->(i) { BOTH[i] } | |
} | |
end | |
end | |
puts FizzBuzz[35] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment