Wednesday, 28 September 2011

Programming Scala without... anything

You don't need all that fancy, modern stuff. A keyboard, a terminal window and the scala command are all you need:

$ scala -e 'println(io.Source.fromFile("freq_list.txt").getLines().map(_.split("\t")(0).toInt).sum)'
71213401
(Prints the result of summing the frequency numbers found in the first tab separated field of file freq_list.txt. The result turned out to be 71213401.)

When the programs get longer, you better stay focused.

2 comments:

Anonymous said...

Thats very nice, but nicer would be if scala -e had similar switches to ruby command line or awk for scripting.

e.g.
automatically splitting on a character and providing this in some variable. access to $FILENAME etc..

Nikolaj Lindberg said...

Dear Anonymous,

I wasn't too serious about programming Scala on the command line (there might be better options, as you point out).

(But I do find Scala useful for small throwaway scripts.)