Functional Programming Principles in Scala

Large icon

I subscribe to the school of thought that learning should never end, and I also believe that as a software developer it’s important to learn new programming languages yearly.  

After learning about Coursera and hearing that Martin Odersky was going to be teach a course covering functional programming titled Functional Programming Principles in Scala, I had to sign up.  The class is a 7 week long introduction to functional programming principals using the Scala programming language.

Course Syllabus

Week One: Programming paradigms; overview of functional programming and the Scala programming language.
Week Two: Defining and using functions, recursion and non-termination, working with functions as values, reasoning by reduction.
Week Three: Defining and using immutable objects, review of inheritance and dynamic binding.
Week Four: Working with collections: Sequences, sets and maps
Week Five: Defining recursive data and decomposition with pattern matching.
Week Six: Reasoning about functions
Week Seven: Case study

Weekly

Each week, on Tuesdays, students are presented with a series of lectures by Martin that cover the week’s topic.  The lectures include prepared slides, writing on a virtual whiteboard and live code examples and run 2-3 hours per week.

After listening to the lectures for the week there are homework assignments which exercise the material reviewed in the lectures.  Pretty common for a college course.

I have to admit the assignments are hard.  I am always up for a challenge with a new language but the assignments go well beyond general Scala syntax or basic functional programming paradigms.  These lectures force the student to learn (or remember) computer science and mathematics from my early days of algorithm and data structures from college.  No complaints here, all really good stuff, but it took some research to recall some of these things.

Conclusion

Would I recommend this class?  You bet!  It was a great class and I learned ton. Scala was a language that I wanted to have a look at and this course was a great introduction but assumed a background in programming and did not cover the basics.  

One things that annoys me is that most programming books start with the very basics of a language, trying to cater to the widest audience possible.  This leaves most books with only 1/2 of their content really unique and usable.

After spending some time with Scala, I decided I was not a fan and won’t be using the language for future projects.  I love the idea of functional programming, lots of small functions with specific functions, but Scala is way too verbose for my liking.  It just looks like functional Java and not different enough from Java or C# for that matter to attract me to use it.  I think Scala drove me to appreciate Clojure much more and I will be writing a lot more parentheses because of it.  

I think it’s good to see other programming languages and styles.  Scala works for some but not all.

Functional Programming Battle

2012 10 03 09 09 21

I’ve been spending some quality time with Scala lately and ran into this research paper comparing Scala, Haskell and F# titled Haskell vs. F# vs. Scala: A High-level Language Features and Parallelism Support Comparison.  It’s a great read and well-worth the time.

This paper provides a performance and programmability comparison of high-level parallel programming support in Haskell, F# and Scala. Developing several parallel versions, we employ skeleton-based, semi-explicit and explicit approaches to parallelism. We focus on advanced language features for separating computational and coordination aspects of the code and tuning performance. We also assess the impact of functional purity and multi-paradigm design of the languages on program development and performance.

It’s interesting to see the three languages compared for their abilities in parallel applications.  It should be noted that the paper is from the Glasgow Parallel Haskell web site so there may be some bias but nothing outstanding.

I like the fact that the comparison is based on a Java VM language, Scala, a .NET language, F#, and a native language of Haskell.  Running on a rich runtime such as the JVM or .NET gives the developer a library of which to draw from, it also adds some overhead.  The results are pretty much what I expected with Scala doing better than I would have envisioned.  The JVM seems to be well-tuned. 

You can download a PDF version of the paper here.   The code used in the benchmarks are also available.

One Night With Clojure Makes a Scala Guy Humble

Really interesting take on using Clojure from a long-time Scala developer over on DZone

The lines of code (LOC) metric is the most interesting to me:

A simple metric generated with cloc, namely Lines of Code really blew me away about Clojure:

  • Java version: 755 lines of code (LOC)
  • Scala version: 419 LOC’s*
  • Clojure version: 57 LOC’s
Yup, that’s right, the Clojure version is more than 10 times less code, and about 7-8 times less than it’s Scala equivalent. In fairness though, the Scala number is a bit misleading: it was written before I got really familiar with Scala, and could probably be condensed to about 150-200 LOC’s. But still, it seems Clojure has a 3-4x advantage on even Scala.

As someone who has been dabbling with Clojure, mainly to learn some functional programming and see what all the hype is about, these details are really interesting and compelling.  It may be time for me to convert part of an application to Clojure and experience for myself.