Scripting with Scala and SBT

来源:互联网 发布:迅捷网络的初始密码 编辑:程序博客网 时间:2024/06/05 04:19

scabl: Scripting with Scala and SBT
http://scabl.blogspot.com/2013/05/scripting-with-scala-and-sbt.html


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 notdoing 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 {
  println("hi")
}

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.
原创粉丝点击