PPL 30 Day 5: Scala

This is part of the Peer Pressure Learning 30 series. To learn more about what in the hell that means, see the intro to the series, A Good Time To Learn.

I have covered all of the data stores I planned to cover. Today, I’m going to finally actually write some Scala. Scala is a general purpose language written atop the JVM. It has features of object-oriented languages, features of functional languages, features of dynamic languages (more or less), and features of statically-typed languages. It tries to cover all bases. It’s interesting.

Installation

You know, I might have to just remove this section. It’s so obvious now, it’s a joke.

brew install scala

Resources

The Scala Site

I slid down the tube that leads to the Scala website. I landed lightly on my feet, looked around a bit, and decided I ought to sit down.

“Holy shit. It has EVERYTHING!”

I was overwhelmed. I’m still overwhelmed. More on that later. Suffice it to say, scala-lang.org probably has everything you need to know about Scala… somewhere in there. It looks pretty, but so does the Cheshire Cat.

Cheshire Cat pic

Scala for Hackers

Tom Morris’ Scala for Hackers appeared on HackerNews just this week. Great timing! Or, suspicious timing, depending on how paranoid you are. The article relates a lot of features of Scala to features of Ruby (and sometimes Java, of course). Obviously, as a Ruby programmer primarily, it was nice to have someone make these connections for me, especially after the Scala site fried my brain.

First Steps to Scala

I honestly don’t remember how I happened upon First Steps to Scala. I’m pretty sure there was a white rabbit involved, someone was late for something, and there was a tea party. I think. In any case, it got me a bit further towards my goal, in less time, than these other resources.

What I Think I Now Know

Listen: There is little chance of me truly knowing one of these items after spending maybe a few hours on each. It is even more unlikely to know a full, general-purpose programming language in that amount of time. We knew that already, right?

Well, then I chose Scala as the first language to tackle, which is a bit like choosing to practice tackling (American football style) for the first time on Andre the Giant.

Andre the Giant pic

My friend and colleague JR has been working with Scala for at least the past few weeks. He told me recently that he has no idea where the rabbit hole ends. I’m pretty sure I just stuck my head in, but already I can’t see the opening through which I entered.

So, now I know that I’m not going to know everything I need to know. Let’s write some freakin’ Scala code.

<% coderay(:lang => :scala, :line_numbers => :inline) do #_ -%> // Simple FizzBuzz object FizzBuzz { def main(args: Array[String]) { for (i <- 1 to 100) { var result = “” if (i % 3 == 0) result += “fizz” if (i % 5 == 0) result += “buzz” if (result == “”) result += i println(result) } } } <% end -%>

I’m sure there’s more idiomatic Scala that could be written. I hope someone adds a comment to point out improvements. That said, I’m pretty happy with that FizzBuzz implementation. Passing over the object line, the for line is a bit interesting. It’s similar to writing for a in [1,2,3] in Ruby. 1 to 100 is the same as if I had written it 1.to(100). In Scala, if a method only takes one parameter, you can call it without a dot or parentheses. The rest of the code is, I think, obvious.

Scala is an interesting language. From what I’ve read, it seems the language was written by some people who are really passionate about programming languages and the functional programming style. The language might have nearly every feature you can imagine (it’s not a Lisp, though ;-). Some of the ideas seemingly borrowed from Ruby (or, perhaps more likely, Ruby’s ancestors) make the language an intriguing one, for me. For instance, all operators are actually methods.

Some ideas, Scala takes a step further. For instance, in Ruby, you can define an instance method %, or <=>, among a few other non-alphanumeric method names that are definable. In Scala, you you can have all available ASCII characters in your method name (with some exceptions, of course!). Want to concatenate two lists? Do this:

<% coderay(:lang => :scala, :line_numbers => :inline) do #_ -%> var a = List(1,2,3) var b = List(4,5,6) var c = a ::: c // results in List(1,2,3,4,5,6) <% end -%>

We also saw what I would dub the in operator above defined as <-. It gets weirder, though. If I recall correctly, I saw JR use an operator that looked like /#/.

Scala also has anonymous functions, mixins, powerful introspection, full object-orientation, and a host of other features that make language nerds’ faces blush. If you’re interested in knowing whether you’re going to actually be able to get things done, read Scala for Hackers. If you decide you want to try it out, read First Steps to Scala. Use the scala website when you need to answer a question the other two missed. Keep on truckin’, but be warned: it’s a long, strange trip.


Tomorrow I am going to jump to JRuby, instead of following the list sequentially. (I really regret making the list an ordered one.) Why? To be honest, it’s a busy weekend, and picking up the differences between JRuby and MRI Ruby will be easier than learning Clojure!

Check out Tim’s PPL 30 Day 5 entry in which he hates on comments. Hey, me too!