Scala: The Good Parts

After reading this email about Scala performance and complexity that a friend told me about on Twitter, I'm more convinced than ever that Scala needs a book titled, "Scala: The Good Parts". I ran across a PHP book with a similar name a year or two ago, and was impressed by the idea of ignoring all the bad stuff in a programming language, and just focusing on the good (as if we could just start over with only the good).

Scala cookbook recipes

I just noticed that I've now written over thirty Scala programming tutorials, so I thought I'd start organizing them here in the form a Scala Cookbook.

I'll clean this up over the next few days, but until then, here's my current collection of Scala FAQs and recipes, in a "cookbook" format.

Scala idiom: Prefer immutable code (immutable data structures)

One great thing I've learned from Scala and functional programming over the last few months is this: 

Make your variables immutable, unless there's a good reason not to.

"Immutable" means that you can't change your variables; you mark them as final in Java, or use the val keyword in Scala. More important than you not changing your variables is that other programmers can't change your variables, and you can't change theirs.

I never gave it much thought before, but if I write a Java method like this:

A simple Scala JSON (Lift-JSON) example

As I continue to plug away on my computer voice control application (SARAH), last night I started working with JSON, specifically the Lift-JSON library (part of the Lift Framework), which seems to be the preferred JSON library of the Scala community.

Scala - Declaring a null var variable (field) before a try, catch, finally block

Sometimes when you're writing Scala code, you need to create a null variable (a var, not a val), such as when you need to declare a variable right before using it in a try, catch, finally block. I just ran into this when writing a Scala IMAP email client, where I needed to create two variables right before the try declaration, so I could reference the fields in the try block and also in the finally block.

Passing a function to a function in Scala (callbacks); cleaning up Java Swing

I've posted a lot of Scala source code examples out here lately, and as I keep trying to learn more about passing one function to another function in Scala (function callbacks), here's another example showing how you can use Scala's functional programming approach to clean up some Java Swing code:

Class casting in Scala

While this may not be the recommended approach, here's how I currently handle class casting in Scala.

In Java you can cast a class like this:

Recognizer recognizer = (Recognizer)cm.lookup("recognizer").asInstanceOf;

As you can see, this is done with the "(Recognizer)" class cast operator.

In Scala you can't use the same syntax, but you can come close, like this:

Scala equivalent of Java .class (classOf)

In Java, you sometimes refer to .class in your source code, like this:

info = new DataLine.Info(TargetDataLine.class, null);

As you can see, I refer to TargetDataLine.class in this code. This works fine in Java, but if you've tried it in Scala, you know the .class part of that code won't work.

In Scala, if you want to use ".class", you instead need to use the classOf function: The following Scala classOf code is equivalent to the previous Java .class code:

A Scala function to list subdirectories in a directory

If you ever need to generate a list of subdirectories in a directory in Scala, here's one way to do it:

def getListOfSubDirectories(directoryName: String): Array[String] = {
  return (new File(directoryName)).listFiles.filter(_.isDirectory).map(_.getName)
}

I intentionally wrote that function in a short, "Scala like" style, but you can expand it to multiple lines, if you prefer.

To demonstrate the use of this directory-listing function, you can print all your subdirectories like this:

How to search multiple jar files for a string or pattern

Here's a shell script that I use that search Java jar files for any type of pattern. You can use it to search for the name of a class, the name of a package, or any other string/pattern that will show up if you manually ran jar tvf on each jar file. The advantage of this script -- if you're a Unix, Linux, or Cygwin user -- is that this script will search through all jar files in the current directory.

Syndicate content