News for hackers. Stuff that matters.
Re:The State of Go by skybrian on Sunday February 05, 2017 @12:43PM (#13573469)
The preference for maps and folds looks like a fad to me. I can use them but I don't think it makes the code any easier to understand, just different.
Re:The State of Go by Groxx on Sunday February 05, 2017 @01:11PM (#13573610)
I dunno, ruby has some nice tendencies here, like `out = [1,2,3].map(&:to_s)` which is small and descriptive[1].

Compare that to:

    out = []
    [1,2,3].each do |x|
      out.append(x.to_s)
    end
IMO that's not fad material - that's progress. Maps and folds can totally be abused to produce inscrutable nonsense, but for small, common operations they're often much more obviously-correct.

[1]: `&:method_name` is extremely-common shorthand for "call this method"

Re:The State of Go by XorNot on Sunday February 05, 2017 @05:20PM (#13575021)
I had no idea what you were trying to do in the first paragraph whereas without knowing much ruby I can easily read the intent of the second.
Re:The State of Go by sheepmullet on Sunday February 05, 2017 @10:56PM (#13576377)
That has more to do with familiarity than anything else.

A tale of two cities is an easier book for an English speaker than Les Aventures de Tintin.

Map applies a function to each item in a collection.

As pseudo-code:

Collection.map(function)

And in the parents example we have:

[1,2,3].map(&:to_s)

The only Ruby specific part is &:.

Re:The State of Go by Cthulhu_ on Sunday February 05, 2017 @02:33PM (#13574091)
It does though. Maps and folds - and functional programming in general - focuses on the /what/. For-loops on the other hand tell the compiler / cpu the /how/.

Summing is a nice example. A naive for-loop will just iterate through the list, keep a variable around, add the next item in the list to it. In functional programming (or with a reduce), you tell it to sum in a more concise way - but more importantly, you don't tell the compiler how exactly it should do it. It's trivial with functional programming to make the task (for example) multithreaded or to use advanced underlying cpu tricks, without you as a developer needing to know how exactly it does what you ask it to.

Re:The State of Go by ridiculous_fish on Sunday February 05, 2017 @09:38PM (#13576124)
Consider product as a counter-example. With an imperative loop, it's easy to add an early-out condition if zero is encountered. But this is harder to do in a (strict) functional language.
Re:The State of Go by Athas on Monday February 06, 2017 @04:21AM (#13577868)
This is exactly why lazy evaluation is often described as control-flow. Lazy evaluation permits efficient composition, without doing extra work (although the constant factors become much larger).
Re:The State of Go by sagichmal on Sunday February 05, 2017 @04:57PM (#13574888)
Yes, that's exactly right. And Go takes a relatively strict stance on making the _how_ obvious to the programmer. It goes out of its way to avoid hiding O(n^k) loops behind language sugar like map or fold, for example.
Re:The State of Go by kibwen on Sunday February 05, 2017 @05:12PM (#13574975)
> O(n^k) loops

I'm unclear where algorithmic complexity is coming into this. Can you clarify what you're trying to imply?

Re:The State of Go by Scriptor on Sunday February 05, 2017 @07:25PM (#13575601)
map doesn't do any magic behind the scenes, it goes through a sequence exactly once.

Can you given an example where map might increase algorithmic complexity like that where the imperative version would not?

Re:The State of Go by sagichmal on Sunday February 05, 2017 @07:55PM (#13575727)
Map hides a loop behind a statement. That's all I meant.
Re:The State of Go by Scriptor on Sunday February 05, 2017 @08:34PM (#13575901)
You can hide a loop behind a function call in Go, Python, really any imperative language.

What makes map different is that it separates the looping mechanics (incrementing, initializing and appending to the collection) from the actual computation we want to perform on each element. It's just separation of concerns.

Re:The State of Go by sagichmal on Sunday February 05, 2017 @09:20PM (#13576074)
Yes. In Go, function and method calls are the primarily, perhaps only, mechanism of abstracting computational complexity. That is, when you see a function or method call, you know that "anything goes" back there, and you need to dig and find out what the computer is gonna be doing. Pretty much everything else is implemented "in plain sight"; in the case of fold/map, that means with a for loop, where you see precisely how many iterations you're going to step through.

You're right: map separates looping mechanics from computation. In Go, that's a bit too much obscurity.

Re:The State of Go by solidsnack9000 on Sunday February 05, 2017 @06:26PM (#13575359)
A 70 year fad.