2 ms·
Do you really prefer a language to not have higher order functions than with them? Quick, what does this do (in Scala)? (1 to 100).map(x => x * x).reduce(_ +
by mruts 7y ago
Do you really prefer a language to not have higher order functions than with them?
Quick, what does this do (in Scala)?
(1 to 100).map(x => x * x).reduce(_ + _) ?
Or if you don't like the underscores we could make it more explicit:
(1 to 100).map(x => x * x).reduce {case (x, y) => x + y }
Let's look at the iterative alternative:
var (i, sum) = (0, 0)
while (i < 100) {
sum = i * i;
i = i + 1
}
You could argue about performance or whatever, but closures are safer, more powerful, easier to read, and more concise.
Using a language without anonymous closures is like programming blind-folded and with one hand behind your back. It's pretty much the definition of Paul Graham's "Blub" languages[1]
Of course, classes are closures and closures are classes. But classes without closures are verbose, stupid, and ugly.
http://www.paulgraham.com/avg.html http://www.paulgraham.com/avg.html
- amedvednikov 7y agosum = (1 to 100).map(x => x * x).reduce {case (x, y) => x + y } sum = 0 for i in 1..100 { sum += i * i } I personally find the second one easier to understand. Performance is much better unless the compiler does some magic to convert the first version to the second one. Adding more logic to the loop also doesn't make the code more complex.