Learning Scala
Writing tests first and leveraging the JVM platform
I love programming and I especially love reading about programming online. I am a longtime Hacker News and Reddit reader and the values of those communities have been impressed upon me. Though these communities can be a little bit clique-y and doctrinaire, I trust their advice because at this point, I know their biases and can interpret how their suggestions might work for me.
One hero of the Hacker News/Reddit programming communities is Rich Hickey.

Hickey has a talk on YouTube that explains the difference between software that is easy to develop but difficult to use, and software that is difficult to develop but easy to use; he talks about hammock-driven-development, where the more time you spend thinking about an interface, the simpler it becomes.
He also has a talk about immutable data and the functions that operate on that data. Immutable data structures do not modify in-place. The practice when you need to make a change to your data is to take in an input data structure and produce a new output data structure based on the rules incorporated in the function and replace the old input data with the new output data.
The argument is that immutable data is easier to reason about. Mutable data structures have the same identity, but different contents at different times, which is confusing. When you are working with immutable data, a given data structure is static and can be referenced again without the need to invert any changes that were made along the way.
So in 2012, Rich Hickey’s videos were helping to popularize Clojure, a functional programming language based on Lisp, but built on the JVM so you could interoperate with any Java programs/services that your company might already use. Working on the JVM also gives you the benefits of Java.util.concurrent which allows you to write
So if I love Rich Hickey’s talks, why not learn Clojure?
Clojure is a beautiful, practical language that is dynamically typed. It needs to be dynamically typed to give it the flexibility of Lisp, where code becomes data and data becomes code. Python is also dynamically-typed and I would like to learn a language that is more distinctive from Python than Clojure. So I decided to learn Scala. Scala is a functional programming language like Clojure, but it is statically-typed. The languageheads on Hacker News and reddit are big on statically-typed languages because defining your types at coding time allows a compiler to catch certain errors that can become a big bear to debug if they are allowed into production. Statically-typed languages are also more efficient to compile because code is more explicit.
So you want to learn Scala, where do you start?
If you’re going this route, that is, trying to learn the “right” language the “right” way, why not go all out with test-driven-development and the Structure and Interpretation of Computer Programs? Test driven development is the ultimate in enterprise-friendly approaches to programming, where everything you write is tested, creating an audit/paper trail for investors and upper-management and facilitating a consistent customer experience. SICP comes from MIT and Berkeley and is the gold standard when it comes to learning a new functional programming language. Luckily, Github has a repo for Scala-Koans, a test-driven approach to learning Scala. In addition, Scala designer Martin Odersky offers an SICP-inspired course on Coursera.

The name of koans are derived from Buddhist life lessons. “What is the sound of one hand clapping?” is a koan, this brief story about reincorporating a wayward soul into a community is a koan. In a programming context, they are tests that you have to rewrite to make them pass. Writing code to make tests pass is a common workflow in many professional programming roles.
If you take a look at the code, you can see a bunch of blanks (“__”). If you look at the top of the file, you can see the BlankValues code that is imported into the program. If you go into the BlankValues file, you can see that the code is set up to throw a TestPendingException whenever it encounters a blank.
So as you are learning, you run the tests, and hit blanks and the compiler/server prompts you to work on the next test. What is a compiler/server? Well, in the first place, it is important to understand that directory hierarchy and building code is very important in the Java world. Tools like Ant and Maven exist to take all of your files from different folders and incorporate them together into something that Java can compile and run. Scala has its own build tool called sbt and to run it on your code as you develop you use a language server. MetaLS is the language server of choice for me, truth be told I am not aware of any others. MetaLS incorporates into your text editor and gives you the features of a full-featured IDE like Eclipse or Intellij. From the official website: “Metals is a Scala language server that supports code completions, type at point, goto definition, fuzzy symbol search and other advanced code editing and navigation capabilities.”
Since I use vim, my pathway for setting up my environment was to download some vim plugins for Scala and put them into my .vimrc file, and then download metals, and configure vim to use metals when I open Scala and sbt files, and then download Scala using Homebrew.
Once I had all of that taken care off, I could clone the scala-koans git repo into my project directory and open the build.sbt file in my text editor. Once I did that, I was solicited by the language server to import the build and then I could exit and run “sbt namaste” the build command defined in build.sbt itself. At that point my code was built and running and I could open another window and edit the test files such as AboutLists.scala. The first window is where the language server is running and it automatically detects when you save changes and recompiles your code, informing you of any errors you may have made.
In this first Scala blog post, I have walked through the motivation for learning Scala and breezed through setting up an environment to run Scala code using Vim, Metals, and sbt. In my next post, I will describe some of the first tests in Scala-Koans and the beginning of the Coursera course.
