5 ms·
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.
by skybrian 10y ago
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.
- Groxx 10y agoI 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"
- XorNot 10y agoI 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.
- sheepmullet 10y agoThat 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 &:.
- Cthulhu_ 10y agoIt 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.
- sagichmal 10y agoYes, 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.
- kibwen 10y ago> O(n^k) loops I'm unclear where algorithmic complexity is coming into this. Can you clarify what you're trying to imply?
- Scriptor 10y agomap 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?
- sagichmal 10y agoMap hides a loop behind a statement. That's all I meant.
- Scriptor 10y agoYou 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.
- sagichmal 10y agoYes. 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.
- solidsnack9000 10y agoA 70 year fad.