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))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.
.getLines.map(_.stripLineEnd).filter(_.matches("[A-Z]{4,}"))
.map(_.toLowerCase.capitalize).foreach(println)
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)