Scripting with Scala and SBT

I wanted to write about a great experience I had with Scala and SBT today. This morning I had to write a script to convert one .csv file into another. I figured, there's no way I'm not doing this in Scala. So I sat down to write my little script. I opened up script.scala in my current directory, and started typing:

object script extends App {

At first, I was entering scalac script.scala and scala script at the command line, but that didn't last long. Ridiculously slow. So I created a build.sbt and wrote:

scalaVersion := "2.10.1"

mainClass := Some("script")

Switch to my compiling window and launch sbt. Enter ~ run. Then it was off to the races! I have a two-key command (C-z C-z) to switch between emacs and sbt. By the time I save in emacs and switch windows to sbt, I'm already looking at the latest output. This is much better turnaround than I used to get in Python or Perl, where I would switch windows and rerun the script after saving a change. Of course, the best part is I'm scripting in Scala and not Python or Perl. I get to use vals and immutable and typed collections. And Option and map and flatMap and foldLeft and for-comprehensions.

I was really pleasantly surprised at how easy it was to get things going in SBT with this.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.