Live data from Hacker News

Why your first Rust FizzBuzz implementation may not work

chrismorgan.info

51–60 of 139 posts

Re: Why your first Rust FizzBuzz implementation may not work

#51
post #42
post #41

Earlier quoted context omitted.

But shouldn't they (String and slice) have some common super type to make this all easy to use ?

Yes. It's a design bug that String and slice don't share a common trait and that `.as_slice()` is common simply to use slice methods. I expect that to be fixed before 1.0.

At least you can now do: foo[].some_slice_method()

Re: Why your first Rust FizzBuzz implementation may not work

#52
post #45

In the contrived Python example, FizzBuzzItem is only called if the result is a number (not in any of the modulo 0 cases) - is that intended? I can see that it works, but it's breaking the analogy for me with the Rust code.

Whoa, that was indeed a mistake. Sorry about that. Fixed.

Thanks, also is the "try it" link on rust playpen linking to the wrong code?

Re: Why your first Rust FizzBuzz implementation may not work

#53
post #43
post #41

Earlier quoted context omitted.

But shouldn't they (String and slice) have some common super type to make this all easy to use ?

Also, the location of storage is slightly more visible (in general) in Rust than in other languages. I have personally found this to be pretty clarifying, because as much a we may like to abstract over it, the location of storage often worms its way into the programming model even in HLLs.

Indeed. I feel like learning Rust has helped me grok C and Java much more, because the different forms of allocation is much are visible.

Re: Why your first Rust FizzBuzz implementation may not work

#54
post #52

Earlier quoted context omitted.

Whoa, that was indeed a mistake. Sorry about that. Fixed.

Thanks, also is the "try it" link on rust playpen linking to the wrong code?

No, that’s the code that is supposed to be there. It’s indicating that there is nothing that you can put as the lifetime there.

Re: Why your first Rust FizzBuzz implementation may not work

#55
post #50

Earlier quoted context omitted.

yeah, no one writes python like this.

I've seen it quite frequently and kindof like it because it doesn't introduce any state that could leak out or get mutated from somewhere else. Although the ternary operator doesn't make as much sense in python as in other languages since there is no const keyword, otherwise that's what the ternary operator is usually used for.

I've only ever seen it non-nested.

Re: Why your first Rust FizzBuzz implementation may not work

#56
post #50

Earlier quoted context omitted.

I've seen it quite frequently and kindof like it because it doesn't introduce any state that could leak out or get mutated from somewhere else. Although the ternary operator doesn't make as much sense in python as in other languages since there is no const keyword, otherwise that's what the ternary operator is usually used for.

I've only ever seen it non-nested.

I’ve certainly written `a if b else c if d else e` before, and it reads perfectly naturally—but you do want to be careful doing such things. They’re very easy to overuse.

Re: Why your first Rust FizzBuzz implementation may not work

#57
post #40
post #20

Putting the String issue aside, I just wanted to show the beauty of pattern matching. for i in range(1i, 101) { match (i % 3, i % 5) { (0, 0) => println!("Fizzbuzz"), (0, _) => println!("Fizz"), (_, 0) => println!("Buzz"), _ => println!("{}", i), } } -- edited: removed `.to_string()`, thanks chrismorgan

I haven't looked too deeply into Rust yet, but was able to understand this coming from Elixir. Pattern matching makes for a beautiful solution. This is a similar solution in Elixir: fizzbuzz = fn(x) -> case {rem(x, 3) == 0, rem(x, 5) == 0} do {true, false} -> IO.puts "fizz" {false, true} -> IO.puts "buzz" {true, true} -> IO.puts "fizzbuzz" _ -> IO.puts x end end Enum.each Range.new(1, num), fizzbuzz Since functions a…

Yeah, it's also fun that you can do it in a multiple function head pattern matchy way

  defmodule FizzBuzz do
    def fizzbuzz(x),          do: fizzbuzz(x, {rem(x, 3), rem(x, 5)})
    def fizzbuzz(_x, {0, 0}), do: IO.puts "fizzbuzz"
    def fizzbuzz(_x, {0, _}), do: IO.puts "fizz"
    def fizzbuzz(_x, {_, 0}), do: IO.puts "buzz"
    def fizzbuzz(x,  {_, _}), do: IO.puts x
  end

  Enum.each Range.new(1, 100), &FizzBuzz.fizzbuzz/1

or in a more ruby-esque fashion

  (1..100) |> Enum.each fn(x) ->
    cond do
      rem(x, 3) == 0 and rem(x, 5) == 0 ->
        IO.puts "fizzbuzz"
      rem(x,5) == 0 ->
        IO.puts "buzz"
      rem(x,3) == 0 ->
        IO.puts "fizz"
      true ->
        IO.puts x
    end
  end

Re: Why your first Rust FizzBuzz implementation may not work

#58
post #5

for i in range(1, 101): print('FizzBuzz' if i % 15 == 0 else 'Buzz' if i % 5 == 0 else 'Fizz' if i % 3 == 0 else i) python doesn't have expressions? oh dear me.

I think that "{true} if {cond} else {false}" is quite an unnatural and confusing construct, especially when you attempt to nest them. Although I'm not really familiar with Python I thought that was concatenating 'FizzBuzz' with the value of some nested ternary expression and only realised the order was inverted when I tried to parse the inner one. The vast majority of conditionals in languages I know follow the {cond…

Python does the same, but it has a 1-line version of conditionals that is what the parent uses. Many Python programmers enjoy 1-liners, but I think once you start adding else statements to them they become unreadable.

Re: Why your first Rust FizzBuzz implementation may not work

#60
post #59
post #5

for i in range(1, 101): print('FizzBuzz' if i % 15 == 0 else 'Buzz' if i % 5 == 0 else 'Fizz' if i % 3 == 0 else i) python doesn't have expressions? oh dear me.

[deleted]

I deliberately didn’t go about omitting the number 15 or the string FizzBuzz, because that would have distracted from the key points I was making about Rust. It is not possible to make it as efficient under those constraints—you end up needing either more than one print call, or to use an owned string, where I was able to end up with a solution that didn’t require any heap memory at all.
Post reply on HN