Sunday, May 19, 2013

Functional Programming Principles in Scala

I just completed the Functional Programming Principles in Scala course at https://www.coursera.org/course/progfun with my solutions at https://github.com/rrevo/progfun . It was a good experience learning both about Functional programming and Scala.
Overall the new features compared to an OO language like Java were-
  1. Higher-order functions
  2. Case classes and pattern matching
  3. Immutable collections
  4. Flexible evaluation strategies: strict/lazy/by name

It was refreshing to try and solve problems without mutation. However this also has a higher performance cost. Most of my current work is in Java and so I can translate some of these ideas to my work. Preferring immutability is a good idea for concurrency anyways and this was also brought up by Effective Java and Concurrency in Practice.

Scala as a language was also pretty interesting. Higher-order functions and the other features made code much more succinct than java. I did find myself trying to minify code into a single line at times. But at the same time this density meant that reading code at a later time was definitely much harder. Since types are also inferred, I did end up spending quite a bit of time trying to reason about the types of variables.

Java 8 will have some form of higher-order functions. That should close the gap between the languages slightly. However scala also has many other features and libraries that also have immense value. Choosing between either still depends on many other factors.