FizzBuzz

Got to talking with Mark Platt about Elixir pattern matching compared to procs in Ruby and he sent me a FizzBuzz demo using procs… which of course made me write one myself.

My goal starting out this time was to do it without conditionals. First attempt was to do it with an array of procs to check against… which produced this mess:

Not a conditional in the bunch. Not pretty — but it’s no real slower than just doing a regular-ish FizzBuzz:

Which led me to wonder if I could get a conditional-less version that wasn’t a complete mess. It took me a bit to remember find (I don’t do a lot of Ruby anymore)… which can act like a complicated-looking ternary if you let it. That led to this:

Technically no conditional… but still basically the same solution only much slower.

Anywho — the point being that being creative led me to write some slow and ugly code.

The next question I had was whether I could write something faster than the standard? Assuming the mod operator was not super efficient and letting tail call optimization do it’s thing I came up with this:

Which is, compared to normalish, kinda slow if maybe a bit hard to follow.

Got anything faster?

Show your support

Clapping shows how much you appreciated Brian E. McElaney’s story.