Hacker News new | ask | show | jobs
by iopq 3426 days ago
I did FizzBuzz in Rust without using ifs:

https://bitbucket.org/iopq/fizzbuzz-in-rust/src/bf4968973d73...

5 comments

I'm more of a fan of the zip/cycle approach:

https://play.rust-lang.org/?gist=3fb51314d7df9249c9f774dde96...

My approach works with arbitrary conditions, that's why it takes closures as arguments. So it's a little bit of a different requirement, I wanted something as general as possible.
I like this approach. The only thing I would criticize is that it uses '' as a substitute for None.

So something like

    fizzes, buzzes, empties = str_cycle('Fizz', 3), str_cycle('Buzz', 5), str_cycle('', 7)
wouldn't work since it would print 7 instead of the empty string
This looks dreadfully complex.
It's all type signatures. The actual function is seven lines.
This looks complexly dreadful.
Here is something similar I did in JS

https://gist.github.com/JakeCoxon/d78fa1debc13e46ae54a

I came up with nearly the same solution in swift:

https://github.com/zachcoyle/fizzbuzz-without-booleans/blob/...

But I like your solution better

You've qualified your approach in various ways downthread, but you can avoid ifs far more easily with two simple enums and two match statements. https://github.com/urschrei/rust_fizzbuzz/blob/by_reference/...
I also wanted to avoid match expressions as well
I'd hate to maintain that.
On the contrary, I'm fascinated by the prospect of a job where I'd be paid to abuse type systems to produce useless programs. :)
On the contrary, once you understand how it works, it's trivial to change the program

Let's say you wanted to change the program to yield "Fizzbuzz" instead of "FizzBuzz" in the 15 case, but the rest being the same.

I would first build my string from "fizz" and "buzz" and then I'd have to do .map(capitalize) as the second to last step. If I wanted to have the correct case in the originating strings I could just run them into to_lowercase first before anything happens and capitalize correctly afterwards.

(Actually, the capitalize function itself is probably more complicated than this change)

Similarly, other changes are intuitively easy to stick into the correct place in the program once you know what this program does.