News for hackers. Stuff that matters.
Re:Rust and Go by alkonaut on Friday November 07, 2014 @04:44PM (#8574795)
If you are considering Go, or just want a good laugh, just read discussions where higher order functions are discussed. Or for that matter, generics.

Here is a gem: https://groups.google.com/forum/#!topic/golang-nuts/RKymTuSC...

There's a chance you'll laugh at the people dismissing higher order functions as nonsense, in which case Go might not be for you. This is a good test of whether you want to try it out or not.

Re:Rust and Go by tptacek on Friday November 07, 2014 @07:12PM (#8575371)
Let me save the reader of this comment a long and unproductive session of reading tea leaves out of mailing list posts:

* Golang doesn't have generics. This comes up so often on the mailing list that it's in the FAQ:

http://golang.org/doc/faq#generics

* Golang has a similar attitude to idiomatic functional programming tools as Python; it doesn't have map() and it's not easy to write a general-purpose map.

(I'll take Golang's lack of generics and map over Python's busted closures, and so call it a draw).

If you want to write programs in functional style, you don't want to use Golang. Pretty simple.

Re:Rust and Go by nightpool on Friday November 07, 2014 @07:53PM (#8575534)
Umm... Am I just interpreting you wrong? Python has map in the builtin namespace:

  >>> map(lambda x: x*5, range(5))
  [0, 5, 10, 15, 20]
Re:Rust and Go by tptacek on Friday November 07, 2014 @08:25PM (#8575643)
Python obviously has those primitives (that's what I meant by the tradeoff between it and Golang) but Guido infamously discourages their use.
Re:Rust and Go by nightpool on Saturday November 08, 2014 @12:39AM (#8576143)
Hmm, sorry I didn't see this earlier, but I personally feel that Python supports a healthy mix of imperative, functional and object-oriented styles. (especially with the (over?) use of the "operators" library I tend to see around nowadays...)

Certainly when I picked up a purely functional language (Racket) after mainly having only Python and Java experience before I didn't feel too disoriented or out of touch.

Re:Rust and Go by burntsushi on Saturday November 08, 2014 @01:17AM (#8576205)
The parent is referring to the fact that Guido van Rossum (the Benevolent Dictator For Life of Python) is pretty down on functional programming in Python. You can read some of the history (from the horse's mouth) here: http://python-history.blogspot.com/2009/04/origins-of-python...

TL;DR - "I have never considered Python to be heavily influenced by functional languages, no matter what people say or think."

Python, IMO, has flutters of functional programming in it. But its broken closures, lack of uncripppled anonymous functions and lack of tail-call optimization are pretty damning strikes against calling Python's support for functional programming similar to its support for imperative or OO paradigms. It just isn't. Yeah, we get `map` and `filter`, big whoop. :-)

Re:Rust and Go by masklinn on Saturday November 08, 2014 @03:07AM (#8576327)
> But its broken closures

They were "unbroken" in Python 3, though because of the way scoping works in the language it requires marking variables (with `nonlocal`)

Re:Rust and Go by burntsushi on Saturday November 08, 2014 @07:30AM (#8576665)
I'm aware. The presence of nonlocal and global still makes them broken to me, even if it's a result of how scoping works. Lua and Javascript both manage to have unbroken closures.
Re:Rust and Go by masklinn on Saturday November 08, 2014 @08:08AM (#8576754)
> Lua and Javascript both manage to have unbroken closures.

Because they use explicit local declaration (and implicitly declared variables are global in both)…

Re:Rust and Go by ufo on Saturday November 08, 2014 @11:48AM (#8577385)
Yes, and I would say that requiring explicit local declarations is the right thing to do. Having a "default" variable scope is very error prone (typos are treated as new variables and closures don't work right) but at least with "global by default" you can use a linter to enforce that all your globals are explicitly declared. In Python its impossible to do something similar.
Re:Rust and Go by masklinn on Monday November 10, 2014 @06:39AM (#8583026)
> Yes, and I would say that requiring explicit local declarations is the right thing to do.

And I could hardly agree more with that, I simply disagree that Python's closures remain broken, they're simply fixed within the constraints set by previous design decisions.

> at least with "global by default" you can use a linter to enforce that all your globals are explicitly declared

Still, there's really no excuse for global as default, there's no convenience justification and could just as easily be an error (more easily really)

Re:Rust and Go by burntsushi on Monday November 10, 2014 @07:36AM (#8583172)
> I simply disagree that Python's closures remain broken, they're simply fixed within the constraints set by previous design decisions.

The fact that I have to distinguish between `global`, `nonlocal` and `default` scope makes them broken. `nonlocal` is a hack.

Saying that it's a result of previous design decisions is a sound technical reason for why `nonlocal` is necessary to make closures work. But as an abstraction, at least, they are broken.

With that, I will concede that they are broken in two different ways between Python 2 and Python 3. This is IMO.

Re:Rust and Go by lmm on Saturday November 08, 2014 @07:47AM (#8576708)
Guido discourages map because it should usually be expressed as a comprehension, no?
Re:Rust and Go by dschiptsov on Saturday November 08, 2014 @05:04AM (#8576477)
Let me clarify even more.) If a language does not threat functions (procedures) as "first-class citizens" (like any other value it could be another function's argument or return value, a member of an aggregate) then you should't use it for writing programs in functional style.

As long as your language has "everything is a pointer semantics" you should be able to write your own map and filter in a 2 or 3 lines of code.

   map(F, [H|T]) -> [F(H)|map(F, T)];
   map(F, []) -> [].
Re:Rust and Go by tptfreak on Friday November 07, 2014 @08:52PM (#8575711)
Question.. do you do anything else in your life other than commenting on HN 24/7?
Re:Rust and Go by tptacek on Friday November 07, 2014 @08:53PM (#8575714)
Yes, I also scrapbook.
Re:Rust and Go by NateDad on Sunday November 09, 2014 @07:17AM (#8579734)
Great response, btw.
Re:Rust and Go by theseoafs on Friday November 07, 2014 @05:24PM (#8574980)
What an infuriating thread. I guess I'm not the kind of person Go is made for, but the fact that everyone in the thread was so close-minded about what might be helpful about map, filter, and reduce was frankly absurd.

I especially enjoyed the demonstration of map would look in Go code, complete with a useless anonymous function:

> bar = map(foo, func(e T) { return f(e) })

And this guy who doesn't seem to understand the concept of function pointers:

> You seem to equate the function f with a single character, which it's not. The user must write it's implementation, which you have omitted for your own benefit.

Re:Rust and Go by one-more-minute on Friday November 07, 2014 @06:42PM (#8575266)
What you're forgetting is that adding one useful feature is a slippery slope towards adding many other useful features, and thereby no longer having a mediocre language.
Re:Rust and Go by alkonaut on Saturday November 08, 2014 @03:11AM (#8576330)
First class functions is a language feature. I definitely understand omitting it from a language. But go has that already. The issue is that without good higher order functions in the core libs, it's quite a waste!
Re:Rust and Go by earlz on Friday November 07, 2014 @05:12PM (#8574931)
Wow that makes me cringe. This specifically:

> The power of the map/filter abstraction starts becoming more apparent when you do things like

> Filter(someFunc, Map(funcWith3args, vecA, vecB, vecC))

> And you realize you should swap map and filter for your particular app for better performance.

met with this reply:

> vs > [10 lines of code for two for loops doing the same thing] > Using range makes it obvious that there is a performance hit in the first place.

Re:Rust and Go by codygman on Saturday November 08, 2014 @12:54AM (#8576167)
In cases of needing more performance, you only swap map and filter for a loop in languages that don't optimize it.
Re:Rust and Go by zzzcpan on Friday November 07, 2014 @06:54PM (#8575307)
There are a lot of bad things in Go, sure. And some are really annoying, like "goroutine all the things" mantra. But no matter how I dislike it there is just no other choice today. It all comes down to support, bug fixing, ease to learn and to use, good standard library, built in cross-compilation and a pretty fast one, static binaries, good enough dependency management, reasonable performance and memory usage, especially in comparison to python/perl/ruby, integrated unit testing, code formatting tool, etc. All these things together matter more, than the language itself.
Re:Rust and Go by grogers on Saturday November 08, 2014 @01:43AM (#8576233)
Except for "static binaries" (which isn't clear what benefits you desire out of them), java has all those things.

The other choices are certainly there.

Re:Rust and Go by NateDad on Sunday November 09, 2014 @07:20AM (#8579738)
Except for "reasonable memory usage".
Re:Rust and Go by Alphasite_ on Saturday November 08, 2014 @12:03PM (#8577441)
And even Java is getting that.
Re:Rust and Go by zzzcpan on Saturday November 08, 2014 @01:41PM (#8577751)
Too late. Also Java is not as easy to learn and to use as Golang.
Re:Rust and Go by waps on Sunday November 09, 2014 @05:38AM (#8579592)
Too bad you can't unlearn functional programming. Or generic programming. Or algebraic datatypes. Or having actual useful datatypes. Or ...

Just the basic thing of sorting a list containing a struct. And the suggested solution by the Go team is a 50 line program. WTF.

They actually claim the err = function(); if err != nil {}. This is the reason I originally left C. And those criticisms really apply to Go as well. Firstly nobody sanely checks those errors, often outright ignoring them. Second when they do, they always just pass the error up (in other words: manually implementing exceptions), or worse, they panic on the error (meaning you cannot trust external libraries won't panic on you, negating the single advantage that leaving out exceptions had). Even if you do check those errors they interrupt the train of thought you have. Imagine someone telling you how to open a door in Go : first, put your hand on the handle. If you don't have a hand then go to the hospital. If there is no hospital near you then go to the car. If there is no car then check for a bike. If the door is locked ... wait what was I doing ?

Thirdly functions that might return a variable or might not ... the official Go team's advice is use a pointer. Sigh. The amount of times I've seen people commit Go code into my program that doesn't check for nil-ness ... Grrrr.

If you don't mind living in the dark ages, then yes, Go is great.

Re:Rust and Go by MichaelGG on Saturday November 08, 2014 @03:14AM (#8576332)
F# has that. With Mono you can produce static binaries. And the F# compiler can also make statically linked assemblies, including the bits it needs from all its dependencies.

Although I'm not sure if there is an F# code formatter tool. Which is made up for by F# being a vastly more capable language.

Re:Rust and Go by kolektiv on Saturday November 08, 2014 @02:29PM (#8577895)
Yes, some code formatting tools exist (within Visual Studio as part of F# Power Tools, and also I believe standalone in some binary form). However, as a community we're currently a little less dogmatic than Go (for example) about what "correct" formatting is.

Tabs vs. Spaces is decided at the language level (it's spaces or it doesn't work) but other things are often a matter of taste. It doesn't tend to matter too much though because (well written) F# code is expressive almost regardless of minor formatting issues (poorly written code in any language is a comprehensibility mess, regardless of "neatness" or newline consistency!)

Re:Rust and Go by kyrra on Friday November 07, 2014 @04:54PM (#8574834)
There are a lot of comments in that thread, is there anything specific you think stands out? Some of Ian's initial comments were saying that they are being very very careful about what gets added to the language.

Also, at this point the language syntax itself is basically locked. And from everything I've seen, they are focusing more on the runtime and tooling before they make any serious changes to the code language.

Re:Rust and Go by alkonaut on Friday November 07, 2014 @05:02PM (#8574888)
I'm thinking of the crowd that insists "map" is never more useful or readable than s straight for-loop. So not only don't they want it in go (which could be understandable in some situations), they genuinely seem to think it has no place in an imperative language. That blows my mind.
Re:Rust and Go by jaekwon on Friday November 07, 2014 @05:08PM (#8574922)
I've written in both paradigms and I mostly agree with the Go team. That it blows your mind blows mine, and I suppose it's good that we have these choices so we don't have to agree.

Could you show me a code snippet of maps and filters used that you believe is more readable than for loops? Maybe I'll get a laugh out of that. :)

Re:Rust and Go by alkonaut on Friday November 07, 2014 @05:35PM (#8575049)
A for loop is a general tool. It can be used for anything. Because of that, it conveys little or no information. As a reader I have to read the loop in detail to see that what it does is perform a mapping, and not something slightly different (the last item could be skipped, etc etc).

The expressiveness of higher order functions comes not from terseness (only) but by expressing intent more clearly.

E.g this expresses (in pseudo) A conversion followed by a filter.

> odd_ints = strings.map(parseInt).select(odd)

This was expressed in the forum, but there was no agreement that this was readable, so I fully expect you to also think the for loop to be more readable. I suspect there is a divide between those who prefer reading how something is done rather than what is intended, in order to understand it.

Re:Rust and Go by jaekwon on Friday November 07, 2014 @07:51PM (#8575527)
Ok, that's a good example. Your one-liner is pretty concise and the intent is clear.

I think on a higher level with complex code, you definitely want better abstractions to convey intent, rather than say just having one large function that does everything.

I think in many cases map/filter does in fact help with readability of intent, especially if you're not declaring the function body inline, and the functions are commonly available ones like parseInt. But generally this isn't the case, which diminishes the readability such that it's comparable to a for-loop anyways.

I spend most of my time trying to figure out exactly that -- the higher level abstractions that aren't easily conveyed by "map" or "filter", that having map/filter generic functions isn't high on priority. I just want a simple language that I can spew out thoughts onto uncompilable code... tweak the code often as I see fit, and work on fixing type errors later and once I'm done with that it will probably run fine with few bugs. The nice thing about for-loops is that it exposes more control points e.g. breaking out early, using the index, inserting log lines -- that the flexibility helps me mutate the code quickly.

So maybe it's more about coding style, rather than readability. Do you like to spec out your code completely before writing things down, or do you prefer to define the spec as you write and edit the code because it helps you get things done faster?

Re:Rust and Go by nostrademons on Saturday November 08, 2014 @02:33AM (#8576297)
Another data point for you (I'm not the grandparent poster):

I prefer to write exploratory code and spec out the design as I go along, and I also prefer map/filter (or list comprehensions) to for-loops.

I suspect it does have to do with coding style, but not in the way you suspect. I like to write very small functions - often one-liners, rarely more than a page - and have each function do one thing and one thing only. I also tend to code mostly bottom-up, figuring out what abstractions I need, writing them, and then writing the functions that use them. So I almost never use an inline lambda for a map, it's usually a function I've already defined.

All of the exploratory scenarios you list are handled by built-in functions in my language of choice (Python). Breaking out early = itertools.takewhile(). Using the index = enumerate(). Inserting the log lines, I'd just insert them in the mapper (although there's also trace).

Re:Rust and Go by NateDad on Sunday November 09, 2014 @07:16AM (#8579732)
Is there a reason you need some built-in map thing and not just user-defined functions? If a loop is too hard to read inline, you slap it in a nicely named function and now it's more clear and more concise. Either way, you have to write the code to do the conversion. select(odd) doesn't work unless you've already written the code behind whatever "odd" is, for example.

I can write go code that makes this line legal:

    odd_ints := parseInts(myStrings).select(odd)
but I'd probably write code so it looked like this:

    odd_ints := odds(parseInts(myStrings))
Is either of these harder to read? Does it matter that parseInts is a "map" and odds is a "filter"? Their function is obvious by their names. If anything, the words "map" and "filter" are extraneous.
Re:Rust and Go by alkonaut on Monday November 10, 2014 @04:25AM (#8582668)
they are so pervasive/important that it should be included in the core library. I'd include my own collection_utils every single time.

That said, having a library function of course requires it to work in a type safe way for all collections/functions, which might make this argument really be one about generics and not about two collection functions. If this is omitted because generics is, I think it's just another argument why omitting generics is making the language simple to the point of being stupid.

Re:Rust and Go by NateDad on Monday November 10, 2014 @07:18AM (#8583119)
Instead of map/filter:

    // using some fake filter/map/lambda syntax
    names := machines.filter(strings.HasPrefix(m.tag, "ec2")).map(|m| = m.name)
why not just write

    names := []string{}
    for _, m := range machines {
        if strings.HasPrefix(m.tag, "ec2") {
            names = append(names, m.name)
        }
    }
What happens when you read that first implementation? Don't you read it as "for each machine, if its tag has the prefix "ec2", then append its name to the list that is returned? Isn't that the exact same thing you'd read the second one as? Except that the second one requires no special knowledge other than loops and if statements.
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.

Re:Rust and Go by klibertp on Friday November 07, 2014 @07:41PM (#8575485)
It's a question of which you prefer: declarative or imperative. Having that in mind, and knowing that "readability" is a tricky concept, here is an example in LiveScript:

    # utility, normally wouldn't be here
    map2 = (f, xs, ys) ->
        map (apply f, _), (zip xs, ys)

    startsWith = (prefix, str) ->
        map2 (==), prefix, str |> and-list

    endsWith = (suffix, str) ->
        revStr = unchars << reverse << chars
        startsWith (revStr suffix), (revStr str)
"<<" is function composition, "|>" is a "pipe" or "reverse application order operator", "_" is partial application. "map", "zip" and "apply" functions are standard and have expected semantics, "chars" transforms a string into an array of chars and "unchars" does the opposite (and they all come from prelude-ls library).

This code is both shorter and easier to read (to me, at least) than equivalent for loops and it also composes better. The details of iteration are irrelevant here and so are abstracted, which means they can be trivially changed.

Of course, for "map" to be this useful it needs to be supported by many functionally oriented language constructs. It's also not the best example for anything, it's just a piece of code I wrote in a style I like, in a language I use.

Re:Rust and Go by jacques_chester on Friday November 07, 2014 @07:20PM (#8575406)
Semantically, loops impose order, maps do not.

A map can be auto-parallelised.

A loop cannot.

It seems to me that for a language aimed at exploiting concurrent features, easy wins for parallelisation would be a feature.

Re:Rust and Go by jaekwon on Friday November 07, 2014 @07:30PM (#8575446)
There's really nothing about a typical map operation that makes it any more parallelize-able than a for-loop, in the presence of closures.
Re:Rust and Go by pcwalton on Friday November 07, 2014 @07:34PM (#8575457)
The article is about Rust. In Rust, the type of the closure indicates whether it can mutate externally visible data (and therefore race on it).
Re:Rust and Go by BrianHV on Saturday November 08, 2014 @12:33AM (#8576133)
Having just started reading the Rust guides, I'm curious: can the closure type actually ensure it doesn't mutate any externally visible data, or does that only apply to memory? Can declare that a closure doesn't (or shouldn't be allowed to) write to disk or hit a database?
Re:Rust and Go by jaekwon on Friday November 07, 2014 @07:59PM (#8575563)
Gotcha! So will Rust then auto-parallelize these "pure" closures?
Re:Rust and Go by pcwalton on Friday November 07, 2014 @08:24PM (#8575640)
We'd like to have generic APIs in the future that will allow idiomatic automatic data parallelization. Niko Matsakis has been thinking about this for quite a while. Stay tuned :)

(Note that Servo has been using this type system feature for a while now to prevent data races in our massively parallel CSS layout code.)

Re:Rust and Go by bjz_ on Friday November 07, 2014 @11:43PM (#8576046)
> Niko Matsakis has been thinking about this for quite a while. Stay tuned :)

Arrg, you have me excited now!

Re:Rust and Go by ufo on Saturday November 08, 2014 @12:10PM (#8577460)
I wouldn't put my hopes very high on this. They tried doing this kind of ubiquitous automatic parallelization in Haskell but they ran into parallelization overhead issues because its very hard to have the computer figure out the correct parallelization granularity all by itself.
Re:Rust and Go by jbooth on Saturday November 08, 2014 @12:41AM (#8576146)
Amdahl's law says auto-parallelising a map operation is usually a big waste of time.
Re:Rust and Go by codygman on Saturday November 08, 2014 @01:03AM (#8576182)
Who is Amdahl, why should I care, and does he expand on this argument or is it your interpretation of a more generalized law?

Going to search for it, but I'd still like to hear your response.

Re:Rust and Go by masklinn on Saturday November 08, 2014 @03:19AM (#8576338)
The application in this case is that the mapped operation generally has a fairly low cost and the (sequential) cost of dispatching and resynchronising back into a result are going to dwarf any gain you'd get unless either the collection is huge (and the parallelization is coarsely chunked) and/or the mapped operation is extremely expensive.

Same reason why even though mergesort is fairly trivially parallelizable there's basically no stdlib running parallel mergesorts by default: you need huge collections before you recoup the synchronization overhead.

Re:Rust and Go by SamReidHughes on Saturday November 08, 2014 @03:54AM (#8576387)
Your collections don't have to be stupifyingly huge for a parallel mergesort to be faster. It's bad for a standard library to auto-parallelize because that's an unwelcome side effect. If you're writing some program where you actually cared about the performance speedup, in most cases having a sort function spawn threads or use threads behind your back in a way that your system can't control is completely unwelcome.
Re:Rust and Go by jacques_chester on Saturday November 08, 2014 @08:19AM (#8576777)
Have you rung up the GPU manufacturers to tell them that they're wasting everyone's time?
Re:Rust and Go by dbaupp on Saturday November 08, 2014 @07:23PM (#8578626)
The synchronisation/set-up overheads are still fairly large (e.g. communicating with the GPU). I find it rather unlikely that e.g. a map over 20 elements will be faster in parallel.
Re:Rust and Go by jacques_chester on Sunday November 09, 2014 @10:55AM (#8580100)
It might not be. But I am happy to ignore the question and leave it to the compiler or runtime to take advantage of easy parallelism. Maps allow that, loops (or any other sequential treatment of data) doesn't.

My personal brand of bigotry is relational databases, so I am accustomed to thinking of sorted / ordered behaviour as the special case. Thinking in sets is very powerful.

It doesn't have to be either/or. Sometimes you need a loop. But it would be nice to have mapping too.

Re:Rust and Go by rybosome on Friday November 07, 2014 @08:08PM (#8575589)
Let's take the example interview question: given a string, how do you determine if it is an anagram of a palindrome? The answer is that, for any given character, at most 1 should appear in the string an odd number of times. Here is an implementation in Scala, with higher-order functions:

    def isAnagramOfPalindrome(s: String): Boolean =
        s.groupBy { c => c }
            .map { case(key, value) => value.size() }
            .filter { n => n % 2 != 0 }
            .size <= 1
And here's a more traditional implementation in JavaScript.

    function isAnagramOfPalindrome(str) {
      var chars = {};
      for (var i = 0; i < str.length; i++) {
        var char = str.charAt(i);
        chars[char] = (chars[char] || 0) + 1;
      }
      var numOdd = 0;
      for (var key in chars) {
        if (chars[key] % 2 != 0) numOdd++;
      }
      return numOdd <= 1;
    }
I find the Scala version much more readable, but I'm assuming you'll prefer the JS version?
Re:Rust and Go by jaekwon on Friday November 07, 2014 @08:50PM (#8575700)
In Go:

  func isAnagramOfPalindrome(str string) bool {

    charCounts := map[rune]int{}
    for _, c := range str {
      charCounts[c]++
    }

    numOdd := 0
    for _, count := range charCounts {
      if count%2 == 1 {
        numOdd++
      }
    }
    return numOdd <= 1;
  }
In this case I think I do prefer the latter. I like how I can name the intermediate objects. :)

Also I don't like the practice of groupBy().map(=>_.size()), which hides the performance penalty of creating arrays. I'm sure a better compiler can do better, but I'd have to know the compiler to assume that.

Re:Rust and Go by carapace on Friday November 07, 2014 @10:14PM (#8575879)
I'll bite. In Python:

    def odd_count(letters):
        return lambda ch: letters.count(ch) % 2

    def anagram_of_palindrome(letters):
        return sum(map(odd_count(letters), set(letters))) <= 1
How's that for some higher-order function action?

But really, unless you are deriving programs by doing algebra you are missing the point of things like map() and reduce() (as far as I know no one is actually doing Functional Programing the way Backus described. Am I wrong? I'd love to be wrong.)

Go read Backus' Turing Award paper: http://web.stanford.edu/class/cs242/readings/backus.pdf

Re:Rust and Go by burntsushi on Saturday November 08, 2014 @12:17AM (#8576107)
The Haskell programmer in me can't resist---eta reduce your functions! :P

    from functools import partial
    
    def odd_count(letters, ch):
        return letters.count(ch) % 2
    
    def anagram_of_palindrome(letters):
        return sum(map(partial(odd_count, letters), set(letters))) <= 1
Re:Rust and Go by masklinn on Saturday November 08, 2014 @11:37AM (#8577345)
A more modern take would use collections.Counter(letters) to extract the letter counts, that avoids traversing the string for each letter count:

    def anagram_of_palindrome(letters):
        return sum(c % 2 for c in Counter(letters).itervalues()) <= 1
Re:Rust and Go by z0r on Saturday November 08, 2014 @08:10PM (#8578720)
The discussion here has been good, and the thread is already old, but I can't help myself!

Maps, filters, folds provide a vocabulary for manipulating entire collections. They allow you to write code for transforming the individual items of a collection without mixing it with code that deals with the shape of your collection. Maps, folds, and filters exist for trees and all kinds of other structures with interesting shapes that are more challenging to traverse and reason about than a simple array or list. Languages with first class support for these operations provide a common vocabulary for processing collections of any shape. This alone is a good enough reason for me to prefer such languages, even without bringing readability into the mix.

I also think that it is usually possible to write code with maps and filters that is at least as readable as its for loop equivalent, especially with the comprehension sugar commonly found in many functional languages.

Re:Rust and Go by twtwtaway on Friday November 07, 2014 @06:34PM (#8575240)
I also agree with the Go team. Although I am a big fan of functional language constructs like map & filter, adding them to Go does not feel right. One of the great things about Go is that it's simple but it does not hide much from you. You still know exactly what is going on (concurrency& channels being an exception). That's why I find it easy to read Go code.
Re:Rust and Go by gruvector on Saturday November 08, 2014 @03:47AM (#8576376)
The go community reminds me of the java community. Blind faith in the design decisions of the language. Any feature it doesn't have is passionately defended as a good decision because the clumsy old way is subjectively clearer, up until the day it gets added.
Re:Rust and Go by masklinn on Saturday November 08, 2014 @11:42AM (#8577365)
I found that attitude much more prevalent in the .Net community: for just about anything added since C# 2.0 (I didn't follow discussions about generics after the 1.0 release though I expect it also happened then), the idea was essentially dismissed as pointless ivory-tower wankery useless to developers in the Real World right until MS announced it for the next version, at which point it became an Obviously Great idea and a good way to trash-talk java.
Re:Rust and Go by grey-area on Saturday November 08, 2014 @10:05AM (#8577026)
That sort of behaviour is in no way unique to Go or Java, and I think it's unfair to judge a language or community by some posts from an online newsgroup - most people are too busy to post or lurk on lists like golang-nuts.
Re:Rust and Go by NateDad on Saturday November 08, 2014 @07:51AM (#8576714)
mapping and filtering are just functions and/or loops. You can write those in go. Making some generic thing to save you 3 lines of (really simple) code is not an incredibly useful use of the Go author's time.
Re:Rust and Go by nemothekid on Friday November 07, 2014 @05:27PM (#8575003)
I don't get the sense that they think it has no place in an imperative language. Like the OP calls out, Go is a very small language with few constructs. For a lot of tasks there are simply only one way to do things. Some would consider this a strength, others a weakness (kind of like Python vs. Perl).
Re:Rust and Go by exacube on Friday November 07, 2014 @06:29PM (#8575222)
I think Ian (who works on Go) summed it up nicely. To paraphrase: yes map/reduce is useful but it's another built-in generic so it adds a lot of complexity and it's not worth the trade-off.

This rounds back into Go's generics debate. If they give in with implementing map/reduce, they might as well start implementing some form on generics.

I think Go is for people who are in-line with the golang crew's views on language design. For the rest, use any other strongly typed language that has sort form of type-abstraction/generics (which pretty much they all do).

Re:Rust and Go by Rapzid on Friday November 07, 2014 @08:50PM (#8575707)
A thousand times this. If somebody were to go through the process of actually trying to implement a generic map function in the current Go they would get a good sense of just what the issues are. Unfortunately you need a good grasp of the type system and this just isn't something most of the commenters on this subject have; the Go community is much more qualified. I've done this and generics are the first road block but after consigning to working around that you run into a lot of Go implementation details around interface{} and type assertion so that what you end up with is actually quite verbose in practise and no more desirable than the alternative.. I decided that for me, for loops aren't so bad.

So it does just come back around to generics but people are solving this with code generation, like gen which looks very interesting, and I imagine that is quite acceptable for the time being, for those who really want it.

Re:Rust and Go by emmelaich on Friday November 07, 2014 @08:15PM (#8575607)
It's useful to know Ian's background too; he's the author of the gold linker (http://en.wikipedia.org/wiki/Gold_(linker) so would be intimate with the sort of problems that generics and map and reduce would introduce to the compilation pipeline.

To a language user the benefits might be clear but the problems not.

To a language implementor the problems will be much clearer.

Re:Rust and Go by Dewie on Friday November 07, 2014 @06:29PM (#8575223)
I liked Rob Pike's driveby comment - programmers these days...
Re:Rust and Go by jfb on Friday November 07, 2014 @08:29PM (#8575652)
... have come to expect some advances in programming languages from the last 50 years of research?
Re:Rust and Go by Dewie on Friday November 07, 2014 @08:46PM (#8575691)
Programming Language Research is Irrelevant, or at least in Pike's world.