Clojure Plugin For Grails

DZone 's Guide to

Clojure Plugin For Grails

· Java Zone ·
Free Resource
According to a post from XML co-creator Tim Bray from awhile back, Clojure is considered "the new hotness among people who think the JVM is an interesting platform" for other languages to build on, for people who think that "there's still life in that ol' Lisp beast," and for "people who worry about concurrency and state in the context of the multicore future."  In the same manner, the Clojure plugin for Grails (created by Jeff Brown) is the 'hotness' for Grails developers who want to write some of their code in Clojure.

With the Clojure Plugin for Grails, a developer can easily execute Clojure code from any Grails artifact.  The plugin requires Grails 1.2 or later.  Let's jump right in and install this thing:

grails install-plugin clojure

The first thing the plugin does is create a src/clj/ directory where the plugin will look for Clojure source files.  The source files should have the .clj extension.  The newest version of this plugin, 0.6, has added support for Clojure 1.2, which had a GA release very recently.  

If src/clj/demo.clj looks like this, for example:
(ns grails)
(def ten 10)
(defn add_numbers [a b]
(+ a b))
Then the add_numbers function can be executed as a method call on a dynamic property named clj from any Grails artifact.  Here's an example:
class MyMathService {
def addNumbers(x, y) {
// invoke the function as a method on the
// clj dynamic property…
clj.add_numbers(x, y)
def addTwenty(x) {
// access a binding (twenty) in clojure via the dynamic
// property and pass it as an argument to a clojure function
clj.add_numbers(x, clj.twenty)
// or use it from groovy
assert clj.ten == 10
The dynamic property is named clj by default, and you can set a custom name explicitly in grails-app/conf/Config.groovy if you assign a value to the grails.clojure.dynamicPropertyName property.

grails.clojure.dynamicPropertyName = 'clojurePropertyName'

You can pass any object to a Clojure function, but when you pass Java or Groovy objects into a Clojure function and start manipulating it, you lose any guarantees that the Clojure language can make for concurrency and immutability.  

You can also get anything back from a clojure function.  The example below would return a persistent list:
(defn getit []
(list "freddie" "shaggy" "scooby"))
Calling this function from Java or Groovy returns a clojure.lang.PersistentList.  Data structures are a very useful component of the clojure libraries because they can be used by Java developers who want to use the data structures but don't want to write in Clojure.

The plugin expects all Clojure functions to be defined in the grails namespace within the your project.  If you want to define functions in other namespaces, the plugin supports syntax that can access those functions.  The code below defines a function in the math namespace:
(ns math)
(defn add_numbers [a b]
(+ a b))
Since it's not in the grails namespace, a special syntax is needed to access it.  Here's an example of how you would specify the namespace with this syntax:
class MyMathService {
def addNumbers(x, y) {
// invoke the function as a method on the
// clj dynamic property and specify the namespace…
clj['math'].add_numbers(x, y)
Here's a short screencast that shows you a basic use case with the Clojure plugin.  Brown gives a special thanks to Stu Halloway, the author of Programming Clojure, for letting him borrow some fibonacci code for this screencast.

A bunch of other useful videos on Clojure are available here.  There's also a Clojure plugin for Griffon that you should check out.

Learn more about the Clojure plugin for Grails here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}