ActsAsFlinn

Aug 3 '10

Scala Stuff I’m excited about

For around the last 5 years I’ve been big into Ruby (Rails and friends).  Lately though I’ve been working on a few projects in Scala.  Scala’s been around for almost a decade but only recently it seems to have developed a larger community and better tools.  As I’ve been working with Scala in my day to day I’m getting really enthusiastic about some of the tools that I think make Scala really great to work with.

So here’s a quick list of what I’m working with and why I like it.

1. Scalate - Scala Template Engine: like JSP without the crap but with added Scala coolness.

As it states, Scalate is a template engine.  It’s somewhat generic though and supports a few different dialects.  I haven’t worked with it yet but it supports a Scala dialect of HAML called Scaml which looks very promising.  I’m a long time user of Haml so this gets me excited.

It also supports Mustache templates which is what brought me to use Scalate in the first place.  I love working with Mustache because it’s a simple and logic-less approach (the right approach) to templates.

2. Circumflex ORM - lightweight Web framework and ORM for Scala

Circumflex is a larger project at it’s core to provide a web framework (inspired by Ruby’s Sinatra micro-framework), Markdown and Freemaker template processing and ORM.  I’ve been working with the ORM which is relatively new so I’ve submitted a few patches where I felt like it was needed.  The ORM package is interesting in that it features a SQL-like Scala DSL.  Here’s an example:


class Country extends Record[Country] {
  val code = "code" VARCHAR(2) DEFAULT("'ch'") NOT_NULL
  val name = "name" TEXT
}

object Country extends Table[Country] {
  def findByCode(code: String): Option[Country] = {
    val co = this AS "co"
    val q = SELECT (co.*) FROM co WHERE (co.code LIKE code)
    return q.unique
  }
}

Obviously a very interesting way to interface with relational databases.  It also supports generic `get` by id and `all` methods Rails style validations, query caching and some other goodies.  It’s an up and coming project so obviously it’s not perfect but given the choice between programming with Circumflex ORM vs JPA I think Circumflex offers a much better programming model </flamebate>.

3. Scalatra (formerly Step) - Tiny Scala web framework, inspired by Sinatra

Scalatra is a micro-framework inspired by Ruby’s Sinatra.  It’s very simple to create named paths via HTTP verbs (GET, POST, PUT, DELETE, etc.) not unlike JAX-RS but with out all the annotations and other malarkey.  Scalatra also supports Scalate so you get Haml and Mustache style templates with only a few lines of code!

Here’s a quick example:


class ScalatraExample extends ScalatraServlet {
  // send a text/html content type back each time
  before {
    contentType = "text/html"
  }

  // respond to '/' with a greeting
  get("/") {
    <h1>Hello world!</h1>
  }

  // send redirect headers
  get("/see_ya") {
    redirect("http://google.com")
  }

  // parse matching requests, saving things prefixed with ':' as params
  get("/date/:year/:month/:day") {
    <ul>
      <li>Year: {params("year")}</li>
      <li>Month: {params("month")}</li>
      <li>Day: {params("day")}</li>
    </ul>
  }
}

4. Cascal - a high-level scala based cassandra library

Cascal is as it’s name and description implies a Scala interface for the NoSQL database Apache Cassandra.  I’ve been testing Cassandra as a high throughput buffer for a write intensive API and so far it live up to the hype.  Cascal seems to offer the most native Scala interface.  Of course there are several others out there, Cliff Moon’s Scromium looks promising.

And there’s plenty more to come.  Coming from the Ruby world I’m finding Scala to be a great middle ground between Ruby and Java.  It brings with it a vast array of high quality libraries that can sometimes be lacking in the Ruby world.  Getting into Scala has introduced me to these great tools and plenty more existing Java libraries.. HawtDB, Play!, Hazelcast, etc.

6 notes Tags: scala scalate scalatra circumflex mustache orm cassandra

  1. kirstie-chilvers reblogged this from actsasflinn
  2. lang-kobold reblogged this from actsasflinn
  3. actsasflinn posted this