News for hackers. Stuff that matters.
Re:The Fastest FizzBuzz in the West by ryanplant-au on Wednesday August 31, 2016 @12:34AM (#12396045)
There doesn't need to be a separate test for 3 and 5, his example already handles it properly -- it's two separate if statements, not an if/elsif, so it'll just meet both conditions and print "fizz" then "buzz" to create "fizzbuzz". What's forgotten is printing the number alone if it's divisible by neither.

Fizzbuzz is one of those problems where there's really no clever hidden golfy solution better than the plain old first-reaction naive approach: an iteration with if/elsif/elsif/else.

Re:The Fastest FizzBuzz in the West by wz1000 on Wednesday August 31, 2016 @06:55AM (#12397343)
> Fizzbuzz is one of those problems where there's really no clever hidden golfy solution better than the plain old first-reaction naive approach: an iteration with if/elsif/elsif/else.

There is if you exploit that monoid homomorphisms exist from any monoidal type to both the Optional and Function types.

My favourite solution is this:

    let (m ~> str) x = str <$ guard (x `mod` m == 0)
    in map (fromMaybe . show <*> 3 ~> "fizz" <> 5 ~> "buzz") [1..100]
This is extensible because you can stick as many rules as you want in there, or even factor them out and make it a function

    (m ~> str) x = str <$ guard (x `mod` m == 0)

    fizzbuzz rules = map (fromMaybe . show <*> mconcat rules)

    fizzbuzz [3 ~> "fizz", 5 ~> "buzz", 7 ~> "bazz"] [1..100]
Re:The Fastest FizzBuzz in the West by wott on Wednesday August 31, 2016 @02:31AM (#12396377)
> Fizzbuzz is one of those problems where there's really no clever hidden golfy solution better than the plain old first-reaction naive approach: an iteration with if/elsif/elsif/else.

There are. With a funky calculation.

For example, taken from Rosetta C version:

  for (int i=0;++i<101;puts("")) {
    char f[] = "FizzBuzz%d";
    f[8-i%5&12]=0;
    printf (f+(-i%3&4+f[8]/8), i);
  }
You might say the calculations are hidden if/else, but well...
Re:The Fastest FizzBuzz in the West by jgh on Wednesday August 31, 2016 @02:50AM (#12396424)
It's possible to do with a switch statement as well (in C).

Edit: Would be nice if the original author of that code snippet threw in some parentheses for a little clarity with the operator precedence.

Re:The Fastest FizzBuzz in the West by flukus on Wednesday August 31, 2016 @07:45PM (#12402289)
Your right, I'd probably dock points for readability though because that's not immediately clear.