I think every vim, emacs and decent IDE user would say "I has a text editor. Your argument is invalid."
I think every vim, emacs and decent IDE user would say "I has a text editor. Your argument is invalid."
array.map(&:to_s)
parses instantaneously. Whereas the procedural equivalent strings = []
for (element in array)
strings.push element.to_s
end
requires scanning through the loop to extract out the logic specific to this case. And it's easy to accidentally overlook something when mentally ignoring the "boilerplate".Not to make a mountain out of a molehill, and not commenting on Go specifically, but the extra "cost" of this style of code adds up when the language itself gets in the way of using high-level abstractions. Methods that are three meaningful lines long are inherently more understandable than equivalent methods that are twelve. Multiply this cost across an entire class, file, and/or project, and it can have a significant impact on one's ability to grok the expected behavior.
More seriously, "for ..." maps into "map", "foldl", "foldl'", "foldr" and perhaps other constructs. They do pretty much the same thing, but I don't necessarily see a real cognitive advantage of either style.
array.map(&:to_s)
eventually means array.map{|str| str.to_s }
As I understand it, the & means convert a variable to a code block. If the object already is a callable object (proc or lambda) ruby doesn't need to do anything to pass it through. Symbols (:to_s) aren't callable so ruby calls the to_proc method on it. The rails framework addd this for Ruby 1.8. The code is below. # File activesupport/lib/active_support/core_ext/symbol.rb, line 10
def to_proc
Proc.new { |*args| args.shift.__send__(self, *args) }
end [1, 2, 3, 4, 5].reduce(0, &:+) #=> 15
Which is short hand for: [1, 2, 3, 4, 5].reduce(0) { |memo, item| memo + item } #=> 15[1, 2, 3, 4, 5].reduce(:+)
[1,2,3].reduce([], &:unshift) #=> [3,2,1]
I don't see a problem with writing a for loop, but I think it's not as composable and it can be a little harder to read. So I don't think it's purely religious but it's close to that. inserts :: [(VarId,VarId)] -> Env -> Env
inserts vvs env = foldr (\(a,b) acc -> M.insert a b acc) env vvs
or inserts(vvs, env) {
for ((a, b) : vvs) {
env = M.insert(a, b, env)
}
return env
}
Pick your poison, but I'll stick with fors. Go blues! vvs.reduce(env){|env, (a,b)| M.insert(a, b, env)}
but the for loop is perfectly readable also because it fits on a single line. I think for longer bodied reduces I might prefer a for loop actually.Not only that, map is composable. Something a for loop never will be.
http://software.intel.com/en-us/articles/automatic-paralleli...
And that does not even address the issue of composability - which is even more important. Map fusion?
bottomsUp(node) {
recurseChildren(node, &bottomsUp);
switch (node.id) {
case FOO:
break;
case BAR:
break;
}
}
At the end of the day the actual code (the switch) is what matters, and the "glue" can be made a couple of lines either way.PS. On a separate language feature, I like match with algebraic datatypes more than switch, but I don't know if Go has match. Go Rust!
strList = (str(x) for x in lst)
There's more complex generator stuff you can do (ie. using the ``yield`` syntax inside functions), but unfortunately I don't know a good resource for that off the top of my head :).