News for hackers. Stuff that matters.
Re:Rust and Go by pcwalton on Friday November 07, 2014 @07:25PM (#8575422)

    let descending_squares = range(0u, 5u).rev().map(|x| x * x).collect();
versus:

    let mut descending_squares = Vec::new();
    let mut i = 4u;
    loop {
        descending_squares.push(i * i);
        if i == 0 {
            break
        }
        i -= 1
    }
(Note that if you try to use a for loop here you will infinite loop due to unsigned underflow.)
Re:Rust and Go by jaekwon on Friday November 07, 2014 @07:28PM (#8575437)
Well this is how I'd write it in Go:

  descending_squares := []uint{}
  for x:=4; 0<=x; x-- {
    descending_squares = append(descending_squares, uint(x*x))
  }
Re:Rust and Go by pcwalton on Friday November 07, 2014 @07:32PM (#8575453)
Use unsigned integers for your index and values. That's what makes it hard to use a for loop. (Sure, you could cast a signed integer loop index to unsigned inside the loop to avoid the underflow problem in this specific case, but I'd argue that the functional style is so much clearer than code that has to work around unsigned underflow gotchas.)
Re:Rust and Go by gorhill on Friday November 07, 2014 @08:54PM (#8575715)
> functional style is so much clearer

There is something that makes me uncomfortable in there:

> let descending_squares = range(0u, 5u).rev().map(|x| x * x).collect();

The overhead. I wonder about it. I am unable to get a sense of what it is. With a simple for-loop, it's rather easy to see it, but with the version above I have no idea. So "much clearer" is not what I see with the piece of code above. I see what it does, but what is not so clear is what code will be generated, something which matters when trying to write efficient code.

Re:Rust and Go by dbaupp on Friday November 07, 2014 @10:51PM (#8575932)
There is not much overhead with Rust iterators at all. They are essentially concrete versions of the deforestation that Haskell can do to avoid constructing intermediate lists, and compile to code that is close to the equivalent C (the functionality is all statically dispatched, so the compiler can inline and optimise the calls). This is a case of having experience with and trusting one's tools.

I wrote a blog post a while ago that used iterators heavily http://huonw.github.io/blog/2014/06/comparing-knn-in-rust/ , as you can see the performance is good.

Re:Rust and Go by gorhill on Saturday November 08, 2014 @10:40AM (#8577151)
I am definitely going to have to look more into Rust.
Re:Rust and Go by kinghajj on Friday November 07, 2014 @10:54PM (#8575935)
Well then, good news! Such usages of iterators in Rust are almost always result in the same code that would be generated by old-fashioned, hand-written loops. The 'trick' is that the methods (and the closure!) are inlined, and generics are specialized at compile time.
Re:Rust and Go by jaekwon on Friday November 07, 2014 @07:58PM (#8575557)
All I did was take your for-loop code and translated into idiomatic Go code. My point is the for-loop in Go isn't as terrible as the loop example you wrote.
Re:Rust and Go by pcwalton on Friday November 07, 2014 @08:16PM (#8575609)
Your version infinite loops: http://play.golang.org/p/uglbTETE6d

See why for loops are tricky? :)

Re:Rust and Go by jaekwon on Friday November 07, 2014 @08:58PM (#8575723)
Doh! I fell straight in :)

Overflows in general are tricky. How does Rust deal with it?

Re:Rust and Go by pcwalton on Friday November 07, 2014 @09:20PM (#8575766)
We have special checked types you can use if you want checked arithmetic. The default is to not check, because CPUs currently make it expensive to check (although I would love it if that could change--we need hardware support though).
Re:Rust and Go by acqq on Saturday November 08, 2014 @04:35AM (#8576442)
But CPU's have the overflow flag, that is actually set if the operation overflows?

You can't write that check explicitly in C, but you can in assembler. Available now, across the different hardware.

Re:Rust and Go by dbaupp on Saturday November 08, 2014 @06:39AM (#8576577)
As you say, detecting the overflow is easy, but efficiently handling it is not. It adds a branch to every single arithmetic operation, and it makes it much harder for the compiler to optimise things e.g. it is hard to vectorise a loop summing an array, if every + has a conditional branch on the overflow flag.

(Also, I believe it introduces a lot of data dependencies, getting in the way of the out-of-order execution of modern CPUs.)

Re:Rust and Go by acqq on Saturday November 08, 2014 @09:14AM (#8576896)
To handle the overflow on the places where you want to handle them, modern processor doesn't have any problem with an additional jump instruction. Also, modern compilers could optimize the checks away if they aren't used. In effect, implementing overflow checks definitely won't turn your C speed (1s) code in a Python speed (40s) code. I estimate it wouldn't be even two times slower in most of the use cases. It's certainly not the problem of the CPU's.

It can be the problem of the certain compilers if they don't have the infrastructure to reason about overflow flags though. But it's not a hardware problem.

Re:Rust and Go by masklinn on Saturday November 08, 2014 @11:26AM (#8577305)
> I estimate it wouldn't be even two times slower in most of the use cases.

In his "We Need Hardware Traps for Integer Overflow"[0], Regher quotes 5% to 100% overhead for languages such as JS or Racket, and that a "highly tuned" checker would likely be in the 5% range. Playing with arithmetics-heavy programs and Rust's checked_* (which are backed by LLVM's overflow intrinsics[1]) I got anywhere from 5 to 40% performance loss IIRC.

That's not a lot, but at the same time when you're competing with languages specifically not paying those 5%, a 5% hit on all computations is not going to get you much love.

Which is why Rust currently lets you do that (via num::Checked* and num::Saturating) but uses overflowing default semantics.

[0] http://blog.regehr.org/archives/1154

[1] http://llvm.org/docs/LangRef.html#arithmetic-with-overflow-i...

Re:Rust and Go by masklinn on Saturday November 08, 2014 @03:15PM (#8578015)
OTOH I must note that Swift, on the other hand, has opted to error on overflow and have a second set of overflowing operators: https://developer.apple.com/library/mac/documentation/swift/...
Re:Rust and Go by acqq on Monday November 10, 2014 @06:28PM (#8587321)
And that's the best approach I can imagine: programmer should be able to control if he wants 5% penalty or speed. It's a win-win.
Re:Rust and Go by dbaupp on Saturday November 08, 2014 @07:05PM (#8578594)
> To handle the overflow on the places where you want to handle them, modern processor doesn't have any problem with an additional jump instruction

This isn't just a jump, it is a branch. Especially when the body of the loop is 6 instructions, adding an extra branch is going to be noticable.

> Also, modern compilers could optimize the checks away if they aren't used

This is equivalent to the halting problem, and most code will not be able to optimise them away. Suggesting otherwise is invoking "sufficiently smart compiler", which is invalid.

In any case, you haven't addressed the problem of missed optimisations (especially vectorisation) caused by having to maintain semantics.

> It's certainly not the problem of the CPU's.

Yes, it partly is: the data dependencies and linearisation caused by checking the CPU flags is bad.

Re:Rust and Go by jaekwon on Saturday November 08, 2014 @12:18AM (#8576109)
Nice. :)
Re:Rust and Go by hit8run on Saturday November 08, 2014 @02:21AM (#8576279)
Just use int and it works fine: http://play.golang.org/p/nZcd3M5aL_

To me golang is readable while even small rust examples don't feel quite right right.

Re:Rust and Go by masklinn on Saturday November 08, 2014 @03:11AM (#8576328)
> Just use int and it

is a completely different piece of code.

Re:Rust and Go by codygman on Saturday November 08, 2014 @01:00AM (#8576175)
I love when arguments are decided by/proven by runnable code examples.
Re:Rust and Go by jksmith on Sunday November 09, 2014 @12:17AM (#8579188)
Or if Go and Rust had a FOR loop guaranteed to stop at 0, as with a Wirthian language, no?
Re:Rust and Go by hammerandtongs on Friday November 07, 2014 @07:50PM (#8575521)
Aside from your specific point (which I liked), isn't there a kind of tension here in a systems language?

For loops are almost always going to explicitly show their allocations whereas the functional version has allocations that are not nearly as obvious.

Re:Rust and Go by pcwalton on Friday November 07, 2014 @07:59PM (#8575560)
None of the iterators in the Rust core library allocate. (In fact, they can't, since the Rust core library has no allocation—it's the building block for liballoc.)
Re:Rust and Go by steveklabnik on Friday November 07, 2014 @07:43PM (#8575494)
You need a collect on that first example, you end with a Vec<uint> in the second, but an Iterator<something> in the first.
Re:Rust and Go by pcwalton on Friday November 07, 2014 @07:49PM (#8575515)
Oops, you're right. Fixed.
Re:Rust and Go by NateDad on Saturday November 08, 2014 @07:25AM (#8576656)
And that's why I like go.

Sorry, that's not really fair. I'm sure the compiler would have caught that when you tried to feed it into something that wanted a vector (or would have not cared if you were just iterating over it).

To be honest, I think Rust has a lot going for it. I just think Go has a lot going for it too... they're just different things. Which is fine, because if everything were the same, the world would be a really boring place. :)

Re:Rust and Go by kxo on Saturday November 08, 2014 @06:20PM (#8578482)
Are you suggesting the Rust compiler didn't catch that?
Re:Rust and Go by NateDad on Sunday November 09, 2014 @07:01AM (#8579714)
No? I don't actually know rust, but I don't think the first thing he typed was invalid code. It just returned an iterator instead of a vector. I was suggesting that when he tried to pass the variable into a function that expected a vector, the compiler would complain. No snark, I just meant what I said.
Re:Rust and Go by kibwen on Sunday November 09, 2014 @07:52PM (#8581484)
To be fair, the line "And that's why I like go" seems to imply that Go is able to keep you from making errors in untested context-free code snippets. Perhaps you meant "and that's why I like that Go doesn't support operations like `map`"?
Re:Rust and Go by NateDad on Monday November 10, 2014 @06:57AM (#8583063)
Well, yes, actually, Go does help prevent errors in untested context-free code snippets... by being really really simple, and not trying to mash a ton of logic into a single line for no reason.

The amount of language trickery is at a minimum with Go, so writing something out in plaintext often just works. It's hard to screw up for loops and if statements for people who have been programming for any significant period of time.

It's not that I like that Go doesn't have map, per se. I just don't miss it. At all. And the fact that it's not in the language means I don't have to read someone else's use of it and try to make sure they're not screwing it up somehow. A wise man once said "It's not that I don't want generics, I just don't want you to have generics."

Re:Rust and Go by kibwen on Monday November 10, 2014 @12:26PM (#8584837)

  > Go does help prevent errors in untested context-free code 
  > snippets... by being really really simple, and not trying 
  > to mash a ton of logic into a single line for no reason.
But there is an example in this very thread of a manually-implemented map-via-a-for-loop from a well-meaning Go user that accidentally underflows into an infinite loop.

This isn't an attack on Go (which I respect as a language for having the temerity to be opinionated, a facet that more languages need to emulate), merely bafflement at your claim that implementing everything anew via bespoke loops is somehow effective at reducing errors.

Re:Rust and Go by NateDad on Monday November 10, 2014 @05:56PM (#8587184)
Yes and that was a carefully constructed example explicitly to show problems with loops.... But underflows in loops are just not a common occurrence, which is why the example works in the first place.
Re:Rust and Go by pcwalton on Tuesday November 11, 2014 @01:31AM (#8588589)
> Yes and that was a carefully constructed example explicitly to show problems with loops

No, it was an example from my experience and a bug I have had to fix multiple times, as I mentioned. I didn't make it up.

Re:Rust and Go by NateDad on Tuesday November 11, 2014 @06:49AM (#8589374)
Sorry, yes you mentioned that. Anyway, this is a stupid thing to argue about and I apologize for my part in it.
Re:Rust and Go by kibwen on Monday November 10, 2014 @07:57PM (#8587651)
Likewise, forgetting a `.collect()` call in Rust code doesn't happen, because the compiler will tell you without fail when it's given an iterator but expects an array.
Re:Rust and Go by pcwalton on Monday November 10, 2014 @02:38PM (#8585730)
> Well, yes, actually, Go does help prevent errors in untested context-free code snippets... by being really really simple, and not trying to mash a ton of logic into a single line for no reason.

Sorry, I don't see the benefit of "preventing errors in code that's never actually executed" as a design goal.

> The amount of language trickery is at a minimum with Go, so writing something out in plaintext often just works. It's hard to screw up for loops and if statements for people who have been programming for any significant period of time.

I just demonstrated a counterexample in this thread.

Re:Rust and Go by tspiteri on Saturday November 08, 2014 @11:20AM (#8577279)
That's the only place in C where I use the postfix decrement operator.

    for (u = 5; u-- > 0; ) {
Re:Rust and Go by NateDad on Saturday November 08, 2014 @07:22AM (#8576649)
Go is not the language for you if you care about mashing as much logic into a single line as you can.

Also, I can count the number of times in my 15 years of professional development experience that I've wanted to count down to zero using an unsigned int as the index... never. Which is not to say that the declarative isn't nice, just saying that the example of the infinite loop is not exactly compelling.

Re:Rust and Go by pcwalton on Saturday November 08, 2014 @09:46AM (#8576986)
> Go is not the language for you if you care about mashing as much logic into a single line as you can.

The point here is that iterators can reduce bugs, independent of aesthetic concerns.

> Also, I can count the number of times in my 15 years of professional development experience that I've wanted to count down to zero using an unsigned int as the index... never.

I like to give that example because it was something I actually hit and a bug that I actually had to fix.

In fact I just hit that again yesterday when iterating over a list in reverse order (painting order for CSS box-shadow).

Re:Rust and Go by zzzcpan on Saturday November 08, 2014 @10:09AM (#8577036)

  > The point here is that iterators can reduce 
  > bugs, independent of aesthetic concerns.
Iterators can reduce bugs, really? This is a very flawed point. Bugs are not caused by lack of language features, they are caused by people. And people make mistakes independent of language features and sometimes because of language features causing cognitive overload or require them to make assumptions.
Re:Rust and Go by masklinn on Saturday November 08, 2014 @11:31AM (#8577320)
> And people make mistakes independent of language features

Unless language features prevent the existence of these bugs. Javascript will blindly let you concatenate a number and a string, Go will not. Therefore you can't make the mistake of unknowingly concatenating a number and a string in Go. Thanks to a language feature.

Iterators prevent indexing mistakes (off-by-one errors, index overflow or overflow, wrong-variable use), therefore iterators can indeed reduce bugs.

Re:Rust and Go by zzzcpan on Saturday November 08, 2014 @11:47AM (#8577384)

  > Therefore you can't make the mistake of unknowingly
  > concatenating a number and a string in Go.
Look at this another way: If you need to do that you now have to think about it and explicitly convert a number into a string. But while you are thinking about it you can make mistake of concatenating a number with a wrong string or make some other screw up, because your thinking power is now reduced.
Re:Rust and Go by masklinn on Saturday November 08, 2014 @12:37PM (#8577537)
> If you need to do that you now have to think about it and explicitly convert a number into a string.

You have to think about it either way, the feature precludes forgetting about it.

> you can make mistake of concatenating a number with a wrong string or make some other screw up

Which you can make in both cases.

> because your thinking power is now reduced.

Your thinking power is not reduced, it's increased: you don't have to wonder whether you should convert something to a string or it already is one, the compiler will tell you, so you can focus better on the actual work at hand.

Re:Rust and Go by zzzcpan on Saturday November 08, 2014 @01:35PM (#8577728)
No, if conversion is implicit you don't have to think which function to call, where to find it and which library to include. Quite significant cognitive overhead I'd say.

Having separate concatenation operator with implicit conversion could reduce that overhead:

  a := "foo" ~ "bar" ~ 123
Instead of:

  import "strconv"
  
  a := "foo" + "bar" + strconv.Itoa(123)
But this is not how Golang guys make decisions. And I'm fine with that nowadays as long as they don't claim to be right in that regard.
Re:Rust and Go by dbaupp on Saturday November 08, 2014 @07:21PM (#8578620)
The interesting code for this example is:

  c := a + b
There's no indication from that line of code what a and b are, a type system that doesn't do implicit conversions will tell you "error: adding string and int" and so the programmer can address the problem (e.g. maybe they meant to parse an integer from the string `a`, maybe they meant to format the integer `b` into a string).

Using literals is not a useful comparison because there is no confusion about types in that case.

Re:Rust and Go by zzzcpan on Saturday November 08, 2014 @09:19PM (#8578855)
My bad, I thought it was obvious from having separate operator for concatenation that '+' operator also has only one intent. So in your case it will parse an integer from a string if operand is a string.
Re:Rust and Go by Dewie on Saturday November 08, 2014 @11:01AM (#8577221)
The way I see it is that you can reduce bugs by increasing the level of abstraction, which in this case means to use things that are more straightforward and less flexible: the ultimate looping-construct is arguably the while-loop, by using a counter to index into the array (if you're working with an array, anyway). But with that flexibility comes more room for error: maybe your increment is wrong, maybe your while-condition is wrong, maybe you inadvertently change the index inside the loop without meaning to, etc. A step over that is to explicitly just say that you want to iterate over each element in the array. You don't get to choose how, but you probably wanted to iterate over it from start to finish anyway. The highest level is a function that encapsulates exactly what you want without having to bother with explicitly iterating over the collection yourself. Now you can't even mess up how you use each member of the array, because that is already handled by the function.

A higher level of abstraction means less flexibility, which means fewer potential things that you can screw up. I don't see how that is a flawed point.

Re:Rust and Go by zzzcpan on Saturday November 08, 2014 @11:31AM (#8577319)
Well, the way I see it has nothing to do with levels of abstractions or flexibility, but everything with reducing amount of brain power required to understand the program. Sometimes abstractions do help, other times they add unnecessary complexities and reduce people's brain power to understand the rest of the code and therefore make mistakes in it. This is all about psychology.

Sorry, if I was rude, I'm just tired of pseudo-scientific language designs.