Live data from Hacker News

FizzBuzz in ten languages

iolivia.me

21–30 of 103 posts

Re: FizzBuzz in ten languages

#21
Java version using higher order functions :)

  public interface Matcher {
    Optional match(int value);

    static Matcher divideBy(int div, String text) {
      return value -> Optional.of(value).filter(v -> v % div == 0).map(__ -> text);
    }

    default Matcher zip(Matcher matcher, BinaryOperator op) {
      return value -> Stream.of(match(value), matcher.match(value)).flatMap(Optional::stream).reduce(op);
    }

    public static void main(String[] args) {
      var matcher = divideBy(3, "Fizz").zip(divideBy(5, "Buzz"), String::concat);
      IntStream.rangeClosed(0, 100)
          .mapToObj(value -> matcher.match(value).orElse("" + value))
          .skip(1).forEach(System.out::println);
    }
  }

Re: FizzBuzz in ten languages

#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 years and I don't have an interpreter installed to verify correctness.

  fb :: (Integer, Integer) -> String
  fb (mod3, mod5)
    | (0, 0)    = "FizzBuzz"
    | (0, _)    = "Fizz"
    | (_, 0)    = "Buzz"
    | otherwise = show n
  
  main = putStrLn $ unlines $ map fb $ map (\x -> (x % 3, x % 5)) [1..100]

Re: FizzBuzz in ten languages

#25

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…

[deleted]

Re: FizzBuzz in ten languages

#26
post #12

Took this as an excuse to write it in pure lambda calculus https://gist.github.com/Tarmean/65da65b8da37bd66d48e96d2aa73... I always enjoy how lambda calculus suddenly becomes a readable language after the prelude: let (\n. let (\m. isZero (mod n m)) \divisibleBy. if (and (divisibleBy 3) (divisibleBy 5)) then FizzBuzz else (if (divisibleBy 3) then Fizz else (if (divisibleBy 5) then Buzz else (intToStr n)))) \fizzBuzzS…

The whole point of fizzbuzz is to filter for people that can just solve a tiny problem in a reasonably short time-- that's it. Most interviewers will even give some slack if the person has to look up the modulo operator or even do with out it. If someone can't do it at all there's some kind of serious issue with the way they're approaching the problem, or perhaps they're just not capable of doing it. But another comm…

It's just a bit of fun, nobody is suggesting you do this in an interview.

Re: FizzBuzz in ten languages

#27
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…

You can do it like that in rust too, so I assume it works in Haskell.

Re: FizzBuzz in ten languages

#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 "$(( $i % 3 ))$(( $i % 5 ))" in
            00)
                echo FizzBuzz
                ;;
            0*)
               echo Fizz
               ;;
            *0)
               echo Buzz
               ;;
            *)
               echo $i
               ;;
        esac
    done

Re: FizzBuzz in ten languages

#29
post #12

Took this as an excuse to write it in pure lambda calculus https://gist.github.com/Tarmean/65da65b8da37bd66d48e96d2aa73... I always enjoy how lambda calculus suddenly becomes a readable language after the prelude: let (\n. let (\m. isZero (mod n m)) \divisibleBy. if (and (divisibleBy 3) (divisibleBy 5)) then FizzBuzz else (if (divisibleBy 3) then Fizz else (if (divisibleBy 5) then Buzz else (intToStr n)))) \fizzBuzzS…

I'm a bit confused, how did you implement let, if, else etc.? I don't really know much about the lambda calculus except for what I learned from a short youtube video.
Post reply on HN