Live data from Hacker News

FizzBuzz in ten languages

iolivia.me

31–40 of 103 posts

Re: FizzBuzz in ten languages

#32
post #22

I've been looking for an example that shows how pattern matching can make code much both compact and readable. I think this does nicely. Here's an example in Scala: (1 to 100).map(i => (i % 3, i % 5) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => s"$i" }).foreach(println) Compare that to the rest of the examples on the page. The only one that comes close in either readability…

The Haskell example is quite similar to the Scala/Rust example as well. I guess you could rewrite the Haskell version to match your Scala version pretty closely as well if you prefer doing the tuple construction and then matching on the tuple, like in your Scala version, instead of doing it directly inside the pattern match. Something like this, with the caveat that I haven't done any proper coding in Haskell in year…

Not exactly write - you would use have to use either view patterns or a case...of statement rather than guards here. But the general gist is right.

Re: FizzBuzz in ten languages

#33

Why or how did fizzbuzz earn its place in lore? I looked at Wikipedia and it didn't seem to go into detail. Also we have the teapot, hello world, Foo Bar What are some others?

It was popularized by Jeff Atwood in his blog entry "Why Can't Programmers.. Program?" https://blog.codinghorror.com/why-cant-programmers-program/

Atwood referenced Imran Ghory who seems to have originated the test.

Re: FizzBuzz in ten languages

#34
Someone should make a tabletop game inspired by FizzBuzz. Or maybe a card game. It should have "bins" as a mechanic. Maybe change the name to include "bin" in it. Throw in 60's science fiction TV aesthetics and 30's gangsters.

Re: FizzBuzz in ten languages

#35

Why or how did fizzbuzz earn its place in lore? I looked at Wikipedia and it didn't seem to go into detail. Also we have the teapot, hello world, Foo Bar What are some others?

Why or how did fizzbuzz earn its place in lore?

The functional why was as a filter for eliminating impostors. At the time it was new, no one could look up the answers for it, because it was such an arbitrary problem. Now, it's just a curiosity.

Re: FizzBuzz in ten languages

#36

Someone should make a tabletop game inspired by FizzBuzz. Or maybe a card game. It should have "bins" as a mechanic. Maybe change the name to include "bin" in it. Throw in 60's science fiction TV aesthetics and 30's gangsters.

The best i can come up with is a Rummy variant. If you need 30s gangsters to play Rummy: The Factor Baron then I have a kickstarter for you...

Re: FizzBuzz in ten languages

#37
post #28

Your bash example could be a little cleaner. Since 0 is truthy in bash and [ `expr something` ] is, for most purposes, (( )). for i in {1..100}; do if (( $i % 3 && $i % 5 )); then echo FizzBuzz elif (( $i % 3 )); then echo Fizz elif (( $i % 5 )); then echo Buzz else echo $i fi done Bash also has a decently powerful matching statement so you can do something similar to the rust example. for i in {1..100}; do case "$((…

icydk, $ is redundant on alpha variable names within $(( ... )

HNY! (:

Re: FizzBuzz in ten languages

#38
post #36

Someone should make a tabletop game inspired by FizzBuzz. Or maybe a card game. It should have "bins" as a mechanic. Maybe change the name to include "bin" in it. Throw in 60's science fiction TV aesthetics and 30's gangsters.

The best i can come up with is a Rummy variant. If you need 30s gangsters to play Rummy: The Factor Baron then I have a kickstarter for you...

I'm making a reference.

Re: FizzBuzz in ten languages

#39
post #4

Crossposting my comment from the blog: The Rust example looks a bit strange, since you're matching on a variable, but don't actually use it. Instead, you could write the match like this: let div_by_three = x % 3 == 0; let div_by_five = x % 5 == 0; match (div_by_three, div_by_five) { (true, true) => println!("FizzBuzz"), (true, false) => println!("Fizz"), (false, true) => println!("Buzz"), _ => println!("{}", x), } ..…

Unless I’m missing something, your second example seems to be the one in the article.

Did the author change it?

Edit: just saw the comments on the article. Yes, she did.

Re: FizzBuzz in ten languages

#40

If you like this may I also recommend [0] which has fizzbuzz in: Java, VBA, php, cobol, forth, C++, brainfuck, python, Groovu, BASIC, JavaScript, CoffeeScript, sh, PowerShell, Clojure, Ruby, C#, Haskell, ABAP, Lua, Matlab, Common Lisp, Oracle PL/SQL, Scala, F#, Bash, C, LOLCODE, golang, Enterprise Java, MySQL, Visual FoxPro, Perl, shakespeare Of course, if you want it in Malboge [1], you can refer to a prior HN discu…

The "Haskell implementation using monoids" on this page is the only solution I've seen where adding a new behavior (e.g. "FizzBuzzWoof") only requires a modification in one spot. (d 3 "Fizz" d 5 "Buzz") becomes (d 3 "Fizz" d 5 "Buzz" d 13 "Woof") eta: And just below it is a similar solution in Python. Cool!

With similar properties, but more disgusting:

  for n in range(1, 101):
      print(n, end='\r')
      if n % 3 == 0:
          print("Fizz", end='')
      if n % 5 == 0:
          print("Buzz", end='')
      print()
Post reply on HN