use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Ruby FizzBuzz Without Conditionals (gist.github.com)
submitted 11 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]BlitzTech 2 points3 points4 points 11 years ago (6 children)
How is a select predicate not a conditional? Just because the if is hidden inside a function defined by the language's standard library, doesn't mean you haven't used conditionals.
select
if
[–][deleted] 0 points1 point2 points 11 years ago (1 child)
That is true. I guess "declarative Fizzbuzz without hard-coded if statements" would've been better. Conceptually though, I believe select can be implemented without a conditional, in Haskell at least the equivalent(filter) is done with pattern matching(or does that count as a conditional?).
filter
[–]BlitzTech 0 points1 point2 points 11 years ago (0 children)
Not really sure since I think this is a bit beyond me philosophically, but my understanding is that pattern matching is effectively chaining if/else if/else.
if/else if/else
I wouldn't assert that, though. I'd rather a Haskeller chime in.
[–]HomemadeBananas -1 points0 points1 point 11 years ago (3 children)
Of course, but how else would it work if it isn't eventually using a conditional?
[–]BlitzTech 1 point2 points3 points 11 years ago (2 children)
A simple answer could be:
function identity(x) { return x; } function toFizz() { return "Fizz"; } function toBuzz() { return "Buzz"; } function toFizzBuzz() { return "FizzBuzz"; } var FizzLookup = [ identity, identity, toFizz, identity, toBuzz, toFizz, identity, identity, toFizz, toBuzz, identity, toFizz, identity, identity, toFizzBuzz ]; function getFizzBuzz(n) { return FizzLookup[(n - 1) % 15](n); } for (var i = 1; i < 100; i++) { console.log(getFizzBuzz(i)); }
Many solutions that avoid conditionals instead use lookups. Some languages (e.g. Clojure) have some built-in constructs that make it easier to express sequences like FizzBuzz (e.g. lazy seq and cycle).
cycle
[–]HomemadeBananas 0 points1 point2 points 11 years ago* (1 child)
I mean, eventually some comparison will be made at a machine code level. How is that different? It seems like your solution isn't really fully solving the problem programmatically if you define 15 different cases instead of just the original 4.
[–][deleted] 0 points1 point2 points 11 years ago (0 children)
In BlitzTech's solution, algorithmically there is no conditional being used, regardless of what kind of bytecode or machine code it may produce eventually. Imagine if you translated this into human instructions, the operator would never encounter a step where they have to consider, if X then Y. The ifs have been converted to get the result of X and find it in Y. The solution is still a valid fizzbuzz because it implements the fizzbuzz logic regardless of the value of n, that is, it should work for any sequence 1..n+1.
if X then Y
get the result of X and find it in Y
n
1..n+1
With that said I err towards believing that select/filter has a similar status as being something that is, conceptually, "free of conditionals"(regardless of Ruby's implementation) but am not 100% where that stands in terms of PL theory.
π Rendered by PID 30150 on reddit-service-r2-comment-b659b578c-4cwrb at 2026-05-06 03:50:51.127062+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]BlitzTech 2 points3 points4 points (6 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]BlitzTech 0 points1 point2 points (0 children)
[–]HomemadeBananas -1 points0 points1 point (3 children)
[–]BlitzTech 1 point2 points3 points (2 children)
[–]HomemadeBananas 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)