I have started learning Scala as part of an independent study project targeting the functional programming paradigm and, to some degree, resource-to-user scheduling algorithms and their NP-complete properties. Now, my background in computer programming languages so far includes Python, PHP, Bash, Ruby and Java. Scala’s syntax is a major departure from what I am used to, primarily because of its focus on the functional paradigm.
But, what is the functional programming paradigm?
I am not the best person to answer this question in full, but I can explain the basics of it. At the core of functional programming is the idea that the entire body of code is data. What this means practically is that every method in the code returns a value. This is important because, in most functional languages, instances of data structures are immutable.
One of the major advantages of this method is that any programmer can follow the functional flow of a program. Immutable variables (a.k.a. constants) can be passed as parameters to a method, but they cannot be modified inside that function. Therefore, in order to make the method meaningful, it must return data that is meaningful that is then assigned to a new data structure instance.
All of this eliminates the “magic” that often happens in code where methods happen to be modifying a data structure instance passed as a parameter. (It’s much worse when there’s a data structure instance defined at the global level, allowing methods to modify them without being passed the reference explicitly.) This forces the code a programmer writes to be much cleaner.
There are a number of very stable and well-honed languages implementing the functional programming paradigm; to name a few:
- StandardML of New Jersey (which happens to be my professor’s language of choice)
- PLT Scheme (now Racket)
And of course there’s a wealth of them on Wikipedia’s Functional Programming page. Scala, however, offers two beautiful features: object-oriented programming (OCaml also has this feature), and compilation down to Java Virtual Machine byte-code. I view these as wonderful reasons to choose Scala as a language to learn. In the end though, the real reason is that it’s an up-and-coming language used by Twitter and FourSquare, so I wanted to jump on the bandwagon and see what the fuss was about.
Where to Start
The worst headache in any language is lack of documentation. Some aspects work like other languages. Other aspects seem to exist in an ether-real sense, but ultimately it’s up to the programmer to implement their full functionality. That’s why there’s always a book or two necessary to get up to speed with a new language. At random, I chose Programming in Scala, not realizing at first that one of the authors of the language, Martin Odersky, was also an author of the book.
The beauty of this is immediately apparent when one starts reading. The book starts with the basic creation of Scala scripts and files and advances all the way through GUI programming, XML parsing, and object equality testing. The last topic includes some very rich scholarly work on how the implementation of object equality tests in most other applications is often lackluster due to the programmer’s misunderstanding of the methodology. The authors give enriching tips on how to test non-reference equality such as giving the
equals method the correct type signature and overriding the
hashCode method for an object.
The book also investigates common syntax in depth, as well. For example, Section 5.3 describes how the compiler translates arithmetic operations into standard methods performed on objects (infixing). This is very useful for programmers to understand how the language is working “under the hood” and how to best utilize such facts to their advantage.
The book itself has really pushed me forward more in Computer Science as a whole than in just Scala. I sincerely encourage anyone with an interest in programming to read it and learn Scala, just because of the beauty of the language and how well designed it is.