News for hackers. Stuff that matters.
Re:FizzBuzz in ten languages by dbrgn on Monday December 31, 2018 @10:10AM (#18794768)
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),
    }
...or even like this:

    match (x % 3, x % 5) {
        (0, 0) => println!("FizzBuzz"),
        (0, _) => println!("Fizz"),
        (_, 0) => println!("Buzz"),
        _ => println!("{}", x),
    }
Furthermore, inclusive ranges can be written like this:

    for x in 1..=100 {
Re:FizzBuzz in ten languages by thanatos_dem on Monday December 31, 2018 @04:40PM (#18797202)
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 by malloryerik on Monday December 31, 2018 @11:59PM (#18799014)
That looks so much like Elixir.

    for x <- 1..100 do
      case [rem(x, 3), rem(x, 5)] do
        [0, 0] -> IO.puts("FizzBuzz")
        [0, _] -> IO.puts("Fizz")
        [_, 0] -> IO.puts("Buzz")
        _ -> x
      end
    end
Re:FizzBuzz in ten languages by CleanShirt on Tuesday January 01, 2019 @04:29AM (#18799544)
Any language that has pattern matching looks similar. Much nicer than if/else/switch.
Re:FizzBuzz in ten languages by frutiger on Tuesday January 01, 2019 @08:17AM (#18800060)
Indeed both of those look so much like ML which predates them by a few decades.