Live data from Hacker News

FizzBuzz in ten languages

iolivia.me

101–103 of 103 posts

Re: FizzBuzz in ten languages

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

FizzBuzz in Coconut looks similar:

  def fizzbuzz(n):
      case (n % 3, n % 5):
          match (0, 0): return "FizzBuzz"
          match (0, _): return "Fizz"
          match (_, 0): return "Buzz"
      else: return n |> str

  (
      range(1, 100)
      |> map$(fizzbuzz)
      |> x -> '\n'.join(x)
      |> print
  )

Re: FizzBuzz in ten languages

#102
post #47
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 "$((…

#/bin/bash for n in {1..100} ; do f="" ((n % 3)) || f="Fizz" ((n % 5)) || f="${f}Buzz" echo ${f:-$n} done

I like it! I was avoiding doing the string append version since the author avoided it.

Re: FizzBuzz in ten languages

#103
post #8

The next question I like to ask in an interview, once they have fizzbuzz working is, "Now make it more efficient."

Zig: const std = @import("std"); pub fn main() u8 { @setEvalBranchQuota(2000); const precomputed_output = comptime fizzbuzz: { var s: []const u8 = ""; var i: usize = 1; while (i Output: $ zig build-exe fizzbuzz.zig $ ./fizzbuzz $ strace ./fizzbuzz execve("./fizzbuzz", ["./fizzbuzz"], 0x7ffeeb1a4540 /* 132 vars */) = 0 write(1, "1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBu"..., 4131 exit(0) = ? The entire program is 1 wr…

> The entire program is 1 write syscall that outputs the answer. I don't think it's theoretically possible to get faster than that.

If stdout happens to be a socket, setsockopt + SO_SNDBUF might help runtime. But that is an extremely byzantine scenario. Another consideration might be a fast CPU attached to a fast but tiny icache and dcache with very slow memory, where reading the pregenerated string is slower than computing it.

Post reply on HN