Live data from Hacker News

FizzBuzz in ten languages

iolivia.me

91–100 of 103 posts

Re: FizzBuzz in ten languages

#91
post #63

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…

Maybe I'm missing something, but how is that any more readable than: for val in xrange(1, 100): if val % 15 == 0: print "FizzBuzz" elif val % 5 == 0: print "Buzz" elif val % 3 == 0: print "Fizz" else: print val

Well, yes, you're right – like beauty, readability is in the eye of the beholder. I've been writing a ton of Scala code lately, and my version is very Scala-like, so it looks better to me.

That said, I think there are several advantages:

• It's more compact, with fewer lines and fewer characters, but at least as readable

• There's simple separation of concerns: it only has a single print statement – the whole range is transformed, then printed – which makes it easy to refactor later if I need to use those values for something other than printing

• It's obvious that all of the cases are handled

• In an amazing coincidence, it looks like code I write :) so I personally prefer it

If you're implying that there are times that if/elif/else syntax is more readable than pattern matching, then yes, absolutely! There are times when one is preferable, and times when the other is.

Pattern matching is especially nice when you need to include conditionals and types:

  vehicle match {
     case car: Car if (car.passengers > 2) => addToHovLane(car)
     case truck: Truck                     => reject("No trucks allowed")
     case _                                => totalTraffic += 1
  }
There's a lot of casting going on there, and it's all handled in the pattern match. Converting this to if/else statements would require a lot of type tests and intermediate variables. Personally, I think that would make things harder to read.

That said, sometimes (often!) the more compact code is, the harder it is to read. I think it's always worth taking a few extra characters to improver readability. So I only use pattern matching with types and discards when I think it makes the code more readable and more flexible (e.g. easier to refactor, improves separation of concerns).

BTW, thanks for pushing back on this. I'm working on the 4th edition of Head First C# (O'Reilly), and C# added syntax very similar to this (borrowed from F#). Writing this reply gave me the opportunity to start thinking through how I want to teach it.

Re: FizzBuzz in ten languages

#92
post #63

Earlier quoted context omitted.

Maybe I'm missing something, but how is that any more readable than: for val in xrange(1, 100): if val % 15 == 0: print "FizzBuzz" elif val % 5 == 0: print "Buzz" elif val % 3 == 0: print "Fizz" else: print val

Well, yes, you're right – like beauty, readability is in the eye of the beholder. I've been writing a ton of Scala code lately, and my version is very Scala-like, so it looks better to me. That said, I think there are several advantages: • It's more compact, with fewer lines and fewer characters, but at least as readable • There's simple separation of concerns: it only has a single print statement – the whole range i…

Pattern matching seems like a useful technique. The languages I use most often don't have it, so I haven't used it much personally. I can see how it would be nice to have for your vehicle example and things like that. FizzBuzz is just so trivial that I don't think the benefits really shine through as benefits and seem more like just alternate syntax.

Re: FizzBuzz in ten languages

#93

The python indentation is inconsistent - two spaces at the first level of indentation, and six spaces at the second level of indentation. I didn't think python would allow this, but apparently it does!

As long as you don't try to change the indentation level within a block, it's allowed. It is, however, a style frowned upon -- you should be consistent and use always the same ammount of indentation for blocks. There are a few styles regarding multiline statements though.

Re: FizzBuzz in ten languages

#94
post #80

Earlier quoted context omitted.

Plus a program to produce the pre-calculated data, to avoid typos. :-)

Why not both at the same time. All you need is a language with compile-time execution: auto fizzbuzz(size_t n) { return iota(n) .map!((i) { if ((i % 15) == 0) { return "FizzBuzz"; } else if ((i % 3) == 0) { return "Fizz"; } else if ((i % 5) == 0) { return "Buzz"; } else { return i.to!string; } }); } void main() { // enum here forces compile-time execution enum fizzbuzz100 = fizzbuzz(100).join("\n"); writeln(fizzbuzz1…

Or:

  $ date +%A
  Tuesday
  $ anyfizzbuzzprogram > fizzbuzz100
  $ cat fizzbuzz100
  [output]

  $ date +%A
  Wednesday
  $ cat fizzbuzz100
  [output]
:-)

Re: FizzBuzz in ten languages

#96
post #82

Fast. Faster. Fastest. The FizzBuzz Gold Standard. Lookup pre-calculated ("hard coded") constants. Don't over-engineer :-): def fizzbuzz [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16, 17, "Fizz", 19, "Buzz", "Fizz", 22, 23, "Fizz", "Buzz", 26, "Fizz", 28, 29, "FizzBuzz", 31, 32, "Fizz", 34, "Buzz", "Fizz", 37, 38, "Fizz", "Buzz", 41, "Fizz", 43, 44, "FizzBuzz", 46, 47, "Fi…

Fast? Yes. The Gold Standard? Pshaw. This thread is utterly incomplete without Fizz Buzz Enterprise Edition™[1]. If you want to get straight to the good parts of it, just look at the Factories folder[2]. [1] https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris... [2] https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...

That's some top notch programming satire

Re: FizzBuzz in ten languages

#98
--A T-SQL version

  Select Case When SomeNumber % 3 = 0 and SomeNumber % 5 = 0 Then 'FizzBuzz'
              When SomeNumber % 3 = 0 Then 'Fizz'
              When SomeNumber % 5 = 0 Then 'Buzz'
              Else Convert(VarChar(10), SomeNumber)
              End As Answer
  From (
      Select Top 100 
          SomeNumber = Row_Number() Over (Order By [object_id]) 
      From sys.all_objects
  ) as Answers

Re: FizzBuzz in ten languages

#99
As seen on reddit recently, Rockstar:

  Midnight takes your heart and your soul
  While your heart is as high as your soul
  Put your heart without your soul into your heart
  
  Give back your heart
  
  Desire is a lovestruck ladykiller
  My world is nothing 
  Fire is ice
  Hate is water
  Until my world is Desire,
  Build my world up
  If Midnight taking my world, Fire is nothing and Midnight taking my world, Hate is nothing
  Shout "FizzBuzz!"
  Take it to the top
  
  If Midnight taking my world, Fire is nothing
  Shout "Fizz!"
  Take it to the top
  
  If Midnight taking my world, Hate is nothing
  Say "Buzz!"
  Take it to the top
  
  Whisper my world
(Yes, that's actually source code.)

https://github.com/dylanbeattie/rockstar

Post reply on HN