Scala, Java, Unix, MacOS tutorials (page 330)

There's a long story behind my new nonprofit business adventure, but in short, I've created the Zen Foundation to fulfill the following mission:

"The Zen Foundation will freely distribute classic Zen books to libraries, schools, healthcare facilities, and other locations where people can discover Zen."

When I say "other locations" in that mission statement, I'm currently thinking of motels, hotels, B&Bs, and many other places where people can "discover Zen".

Update: This article was written for Scala 2.9. Things changed a little bit in Scala 2.10, so see this new article, Creating implicit methods in Scala 2.10, for correct examples for 2.10 and newer versions of Scala.

A cool thing about implicit conversions in Scala is that they let you add new methods to existing classes, including existing Java and Scala classes such as String, File, and so on.

Scala XML FAQ: How do I load an XML URL in Scala? (How do I read/download the contents of an XML URL in Scala?)

To load the contents of an XML URL (web page) in Scala, such as an RSS news feed or RESTful web service, just use the load method of the Scala XML class:

val xml = XML.load("http://www.devdaily.com/rss.xml")

Here's an example of what this looks like in the Scala REPL:

Scala FAQ: How do I load an XML file in Scala? (How do I open and read an XML file in Scala?)

I demonstrated this in my earlier Scala XML - Searching XMLNS namespaces, XPath tutorial, but you can load an XML file in Scala like this:

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.

Depending on your personal preference, or perhaps the needs of the moment, you can use a C#-style "curly brace" package syntax in your Scala applications, instead of the usual Java-style. As a quick example of what this looks like, here are a couple of simple package names and classes:

Scala offers a cool feature where you can rename a class when you import it, including both Scala and Java classes. The basic syntax to rename a class on import looks like this:

import scala.collection.mutable.{Map => MMap}

and this:

import java.util.{HashMap => JavaMap}

If all you needed to know, I hope those "rename on import" syntax examples were helpful.

Scala URL FAQ: How do I download the contents of a URL to a String or file in Scala?

I ran a few tests last night in the Scala REPL to see if I could think of different ways to download the contents of a URL to a String or file in Scala, and came up with a couple of different solutions, which I'll share here.

Download URL contents to a String in Scala

The best way I could think of to download the contents of a URL to a String looks like this:

Scala Map FAQ: How can I iterate/loop over a Scala Map?

There are several different ways to iterate over a Scala Map, and the method you choose depends on the problem you need to solve.

To get started with our examples, let's create a simple Map we can work with:

[toc hidden:1]

At the time of this writing, you can't easily find the Scala Process and ProcessBuilder classes (the Scala API documentation), so in an effort to help you (and the search engines) find those classes more easily, here are direct links to them:

In Scala there are no ++ or -- operators. You should instead use the += and -= operators, as shown below. First the += operator:

scala> var i = 1
i: Int = 1

scala> i++
<console>:9: error: value ++ is not a member of Int
              i++
               ^

scala> i += 1

scala> println(i)
2

Next the -= operator:

Scala exec FAQ: How do I execute external system commands in Scala?

When it comes to executing external system commands, Scala is a dramatic improvement over Java. The operators Scala makes available are much more like Perl or Ruby, and the operators themselves are consistent with traditional shell commands, and are therefore easy to remember. Let's take a look at a few examples.

When you want to test a multiline command/statement in the Scala REPL, you can easily run into a problem where the REPL gets confused, or more accurately, it attempts to evaluate your statement too early.

As a simple example of this, imagine that you want to test the Scala "if" statement syntax. You begin typing your if statement normally, but when you hit [Enter] after your second line, you'll see that everything blows up:

A great thing about Scala is that not only is it scalable, it was also created to help you work on small tasks, including being useful in shell scripts. This includes small shell script tasks like prompting a user interactively from a shell script, and reading their input.

You can prompt users with print commands like println and print, and you can read their input with all of these methods that are available on the Scala Console class:

When you're working in the Scala REPL and want to see what methods are available on a class/object, you can create an instance of an object, follow that with the "." character, and then press the [Tab] key. This process, known as "tab completion" in the REPL, gives you a preliminary list of methods that can be called on the object.

Here's what this looks like when we try it on an Int object:

While working with a Scala Actor last night, I came across a situation where I wanted to be able to manually tell the Actor to quit/die/terminate.

It looks like the proper way to exit a Scala Actor is pretty simple:

Scala FAQ: How do I convert a String to Int in Scala?

Solution: Use 'toInt()'

If you need to convert a String to an Int in Scala, just use the toInt method, which is available on String objects, like this:

[toc hidden:1]

With the Facebook IPO coming up this week, I thought I'd take a quick look at recent IPOs from other tech companies. These charts generally show the early months after an IPO.

With thanks to Yahoo for the graphics, here are the charts:

Angie's List:

CafePress:

Google:

Just a quick note here today on how to show methods from the String class in the Scala REPL, as well as methods from the StringOps class, which aren't seen as easily.

First, if you've used tab completion in the REPL, you may already know that you can show many String class methods in the REPL. If you hit the [Tab] key once you'll see a short list of String methods:

I was just reading the Scala man page, looking for something else, when I ran across this tip on how to speed up the execution of Scala shell scripts, using the savecompiled flag of the scala command: