Last active
August 29, 2015 14:16
-
-
Save yoshikischmitz/f0a768d2e6239dc7d710 to your computer and use it in GitHub Desktop.
A highly extensible, declarative fizzbuzz with no hardcoded if statements, and no mutation.
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
applicators = {3 => "fizz", 5 => "buzz"} | |
fizzbuzz = | |
(1..100).map do |n| | |
fb = applicators. | |
select{|a| n % a == 0}. | |
values.join | |
[n.to_s, fb].max # "1" > "" and "fizz" > "100000" since "1" < "a" | |
end | |
puts fizzbuzz |
I figured out the same exact thing (description and all) about a year ago, albeit differently, using pure functions only
Here is my attempt at the same thing. Self is a fizzbuzz module - I have a variety of solution attempts...
def self.no_conditionals
fizzy = [1,0,0]
buzzy = [1,0,0,0,0]
1.upto(100) do |i|
fizzy.rotate!
buzzy.rotate!
str = i.to_s * (1 - (fizzy[0] | buzzy[0]))
str << "Fizz" * fizzy[0]
str << "Buzz" * buzzy[0]
puts str
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool idea. I came up with 2. Ruby really needs an
Integer#divides?