Code Watch: Two years with Scala
October 22, 2012 —
(Page 1 of 3)
Related Search Term(s): functional programming, Scala
For the past two years, my day-to-day programming language has been Scala. Scala is a hybrid language that combines the approaches of object-orientation and functional programming. You can treat Scala as a "Java.next" with minimal changes in your object-oriented mindset (the only thing that springs to mind is that Scala's "companion objects" might seem like unnecessarily clunky replacements of Java's "static" functionality). Even just as a drop-in replacement, I think most programmers would prefer Scala to Java: type inference saves finger-typing, and fields and class structure are a little clearer.
Scala is also a functional programming language. I've talked about functional programming many times over the years, but it continues to have an undeserved air of mystery around it. The low bar for a language to be "functional" is the support of functions as "first-class constructs." Essentially, wherever you can have a value (on the right-hand of assignments, as the input or output of functions, etc.), you can have instead an anonymous block of code (if you like Greek, you can call them "lambda functions"). This turns out to be so convenient that every mainstream language either already supports or is moving toward such support; every language is, or will be soon, a "functional programming" language.
The greatest practical benefit of first-class functions is that, multiple times per day, instead of writing a for loop that iterates over elements of a collection to transform, filter or accumulate something, you just pass a small function to common collection-class functions such as (in the Scala world), map, filter and foldLeft. I cannot imagine a programmer not preferring the use of such functions: They are both clearer and more concise than loops.