3 ms·
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 coul
by dbrgn 8y ago
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 {
- thanatos_dem 8y agoUnless 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.
- malloryerik 8y agoThat 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
- CleanShirt 8y agoAny language that has pattern matching looks similar. Much nicer than if/else/switch.
- frutiger 8y agoIndeed both of those look so much like ML which predates them by a few decades.