Thursday 18 December 2008

Scala for small throw-away scripting tasks

I've come to use Scala for tiny scripts to be thrown away after doing some small task. Typically this involves processing a few files, comparing some textual data, maybe extracting some fields of tab-separated files, etc. The kind of things that Perl used to be the obvious choice for.

Although lacking Perl's simplified syntax for iterating over all lines in files, Scala works quite nicely for small tasks.

For example, today I had to extract from a file all lines of four or more characters including only upper-case characters, and capitalize the output:

scala.io.Source.fromFile(args(0))
.getLines.map(_.stripLineEnd).filter(_.matches("[A-Z]{4,}"))
.map(_.toLowerCase.capitalize).foreach(println)
Not exactly a thing of beauty, but it only took a minute and it works. And it reminds me a bit of a classic Unix command line pipeline.

A few things on my wish-list to make Scala even better for small scripts:
  • A nicer way of setting the output character encoding (currently you have to do something like Console.setOut(new java.io.PrintStream(Console.out,true,"UTF8")))
  • It would be great if Source.getLines could remove the new line character of each line
  • A better name for RichString.stripLineEnd (for some reason, it is totally impossible for me to remember the name of this method)
  • Maybe scripting support in the Scala Netbeans plugin? (Currently, I think the plugin wants you to put your code in a class/object)

3 comments:

Unknown said...

Have you looked at scalax? I use it when I write my little scripts. I like how it handles IO when compared to the normal library.

My wish list for scripting is:

1. Library to dynamically invoke methods on objects
obj -> length or somesuch to run the length method
2. Easy way to run processes.
"svn co http://xxx".run

Nikolaj Lindberg said...

Jesse,

no, I've never looked at scalax, though I have seen it mentioned a number of times in the mailing lists. I'll take a look. Thanks for the tip!

/nikolaj

Seth Tisue said...

see http://www.nabble.com/Re:--scala-user--Two-years-loving-Scala-tc20545116.html for a little collection of code I wrote to address some of these issues. (I probably ought to be using scalax, though!)

btw, beware of
https://lampsvn.epfl.ch/trac/scala/ticket/1514. I've been doing all my scripting in Scala but had weird flakiness with large files; eventually I figured out this bug was the culprit. It's fixed in Scala 2.7.3.RC1.