Posts in the “scala” category

How to add a new jar file to the Scala REPL classpath (interactive command line)

Scala REPL FAQ: How do I add a Jar file to the Scala REPL classpath? (The Scala REPL is the interactive command line you get if you just type scala at your command line.)

To add a new jar file to the Scala REPL classpath (interactive command line classpath), use the :require command at the command line, like this:

scala> :require myjar.jar

How to print X number of characters in Scala (repeat that character)

If you ever need to print 25 blank spaces in Scala, this works:

println(" " * 25)

That’s pretty cool (and convenient) that you can repeat X number of characters with such a concise syntax like that. Of course you don’t have to just print the characters out, you can also assign them to a value and then use them however you want to:

val x = "a" * 10

Initialize Scala variables with Option, None, and Some (not null)

Summary: How to properly use the Scala Option/Some/None idiom to initialize empty var fields -- and specifically how not to use null values for the same purpose.

When you get started in the Scala world, you quickly learn that null values are a bad thing. Scala makes it easy to replace null values with something better, and that something better is what I call the Option/Some/None pattern (or idiom).

How to stop an Akka actor (and shutdown the Akka system)

Akka actor FAQ: How do you stop an Akka actor?

I don't have time this morning to write my usual tutorial, so in short, if you want to stop an Akka actor, use code like this from inside your actor's receive method:

context.stop(self)

Or, if you want to shut down the Akka system, use the following code, again from inside the receive method of one of your actors:

How to use multiple generators in Scala ‘for’ expressions (loops)

A cool thing about Scala for loops — what I’ll more-accurately call for expressions in this article — is that you can have multiple generators. What’s also very cool about them is how they work.

For example, imagine that you have these two values:

val nums = Seq(1,2,3)
val letters = Seq('a', 'b', 'c')

An interesting question then becomes, “What is the type of res in this expression?”:

Researching the use of an IO Monad in Scala

As I was researching who might be using an “IO Monad” in Scala, I found this quote from Martin Odersky in the Google Group titled “scala-debate”:

“The IO monad was a neat trick for combining side effects with lazy evaluation ... there is only one lazily evaluated language in wide usage today and even its creators have said that laziness was probably a mistake. Strict languages don’t need the IO monad, and generally don’t have it, even though they could. Bob Harper’s posts in his ‘existential type’ series are a good explanation on why not.”

Here’s a link to Bob Harper’s The Point of Laziness article.

Scala: Reassignable variables and properties (def fields)

Sadly, I had to get away from Scala for a while, but now I can get back to it again. Just as I started getting back into it I happened upon the following code, and thought, “Well, surely title in this anonymous class is a var field. How strange that the Programming in Scala guys would use a var like this.”:

Mutable Scala arrays (adding elements to arrays)

Just a quick note today that if you want to create a mutable Scala array -- particularly an array that can grow in size after you first declare it -- you need to use the Scala ArrayBuffer class instead of the Array class, which can't grow.

Here's a short example of how to instantiate an ArrayBuffer object, then add elements to the array:

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.

A collection of Scala XML tutorials

The following links are a collection of Scala XML tutorials I've written. Most of them come from the Scala Cookbook, while the others were written before I wrote the Cookbook.

Without any further ado, here are the links:

Scala XML examples: XML literals, mixing XML and Scala source code, XPath searching

A really terrific feature about Scala is that XML handling is built into the language. This means you don't have to deal with XML as String objects, you deal with it as XML objects.

Here are just a few examples of using XML in Scala. First, you can create an XML literal like this:

scala> val hello = <p>Hello, world</p>
hello: scala.xml.Elem = <p>Hello, world</p>

Again, note that this is not a String, there are no double quotes; we've just defined an XML literal in Scala.

How to extract data from XML nodes in Scala

Problem: In a Scala application, you want to extract information from XML you receive, so you can use the data in your application.

Solution

Use the methods of the Scala Elem and NodeSeq classes to extract the data. The most commonly used methods of the Elem class are shown here: