News for hackers. Stuff that matters.
Re:Why your first Rust FizzBuzz implementation may not work by Hello71 on Sunday October 12, 2014 @09:48PM (#8447198)

    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.
Re:Why your first Rust FizzBuzz implementation may not work by burntsushi on Sunday October 12, 2014 @09:51PM (#8447201)
The OP's characterization is reasonably accurate in my experience. I've run into several Python programmers who didn't actually know about Python's special "if expression" syntax.

Moreover, the code you've presented here certainly isn't idiomatic, which counts for something.

Re:Why your first Rust FizzBuzz implementation may not work by Hello71 on Sunday October 12, 2014 @09:55PM (#8447206)
sure, but when you're implicitly comparing code segments (by placing them next to each other), you should at least make the effort to make them more the same, instead of pointing out that one language is missing a feature used in the other language, especially when this claim is false.

the formatting can of course be improved:

    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)
there's also a suspicious "return" at the end of the second code segment which mysteriously appeared some time after the first one; looks like the author was trying a little too hard to differentiate python and rust.
Re:Why your first Rust FizzBuzz implementation may not work by burntsushi on Sunday October 12, 2014 @09:58PM (#8447212)
I disagree. I think code comparisons should be done using idiomatic code. I personally would not consider chaining `if` expressions like you've done here idiomatic Python.
Re:Why your first Rust FizzBuzz implementation may not work by Hello71 on Sunday October 12, 2014 @10:05PM (#8447230)
and yet people write:

    let result = if i % 15 == 0 {
        "FizzBuzz"
    } else if i % 5 == 0 {
        "Buzz"
    } else if i % 3 == 0 {
        "Fizz"
    } else {
        i
    };
in rust? either this is good, readable code or this is poorly written, unintelligible code. you cannot make the argument that sometimes it is readable and sometimes not based on the presence of braces.
Re:Why your first Rust FizzBuzz implementation may not work by dbaupp on Sunday October 12, 2014 @10:17PM (#8447250)
The comparison is not "readable", it is "idiomatic". (I'm not saying either is unreadable, just pointing out that you're attacking the wrong thing.)
Re:Why your first Rust FizzBuzz implementation may not work by burntsushi on Sunday October 12, 2014 @10:47PM (#8447305)
Using `if .. else if .. else` in Rust as an expression is absolutely idiomatic.
Re:Why your first Rust FizzBuzz implementation may not work by rplnt on Monday October 13, 2014 @05:10AM (#8448008)
I don't see a problem with code chosen for each example. What is a problem is explicitly stating that python doesn't have such functionality.
Re:Why your first Rust FizzBuzz implementation may not work by burntsushi on Monday October 13, 2014 @06:55AM (#8448199)
Yes. I responded to this initially above.
Re:Why your first Rust FizzBuzz implementation may not work by adamnemecek on Sunday October 12, 2014 @09:58PM (#8447214)
yeah, no one writes python like this.
Re:Why your first Rust FizzBuzz implementation may not work by Too on Monday October 13, 2014 @12:36AM (#8447500)
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.
Re:Why your first Rust FizzBuzz implementation may not work by adamnemecek on Monday October 13, 2014 @12:50AM (#8447530)
I've only ever seen it non-nested.
Re:Why your first Rust FizzBuzz implementation may not work by chrismorgan on Monday October 13, 2014 @12:53AM (#8447535)
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 by userbinator on Sunday October 12, 2014 @10:20PM (#8447255)
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} {true} {false} order: IIf({cond}, {true}, {false}) in VB, SQL, and spreadsheets; if({cond}) {true} else {false} and ternary {cond}?{true}:{false} in C and C-derived languages; (if {cond} {true} {false}) in the Lisp family; if {cond} then {true} else {false} in ALGOL/Pascal, etc. There's probably a reason for this order, as seeing a condition in the middle of an expression feels surprising and unexpected.

Re:Why your first Rust FizzBuzz implementation may not work by wodenokoto on Monday October 13, 2014 @01:43AM (#8447630)
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.
[deleted]
Re:Why your first Rust FizzBuzz implementation may not work by chrismorgan on Monday October 13, 2014 @01:53AM (#8447650)
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.
Re:Why your first Rust FizzBuzz implementation may not work by YZF on Monday October 13, 2014 @01:55AM (#8447655)
yeah. I'm being too nitpicky. I guess part of the "beauty" is that 3*5==15 but I realized that you'd need more complexity after hitting the reply button.