Clojure Web Frameworks Round-Up: Enlive & Compojure
Clojure is rather new member of the LISP family of languages which runs on the Java platform. Introduced in 2007 it has generated a lot of interest.
Unlike most JVM languages, Clojure is not object-oriented. but provides things you want from OO like:
- encapsulation (via namespaces, private definitions and closures)
- polymorphism (multimethods)
- functional reuse instead of inheritance.
In the last years there have been many web frameworks and libraries build with Clojure like:
InfoQ had a small Q&A with James Reeves and Christophe Grand, the creators of Enlive & Compojure, about their projects and their experiences working with Clojure:
InfoQ: Would you like to tell us a little bit about yourself and how you started working with Clojure?
Christophe (Enlive): I'm a independent consultant, I live in France and I've been spoilt by early exposure to functional programming. Before discovering Clojure I have been working for months on a real-time collaborative text editor for the legal department of a customer. This software was written in Rhino and it was an exercise in defensive programming. Once this gig over I looked for a better language, a "five-legged sheep" -- a rare bird in French -- and whose five legs were: "dynamic" typing, functional programming, meta-programming, strong concurrency story, hosted on the JVM. I was ready to sacrifice one leg (requirement) of the sheep when I stumbled on Clojure which had been around for 4 months at the time. Once the robustness and the soundness of the language and its implementation assessed it became my default language.
James (Compojure): I'm a British developer currently living in London. My interest in Clojure came about a few years ago when I was looking to learn a Lisp, and Clojure happened to be released around that time.
Clojure was simple enough that its core libraries were relatively easy to learn, and I had become a fan of immutability and functional programming after programming in Haskell for the previous year. I'm also a big fan of languages with a straightforward syntax and a small but powerful standard library, so Clojure instantly appealed.
InfoQ: What is the single feature of Clojure that you find most useful in your every day work?
Christophe (Enlive): Its sane state management: immutability by default and efficient persistent data structures. Unlike other languages which allow for a functional style, Clojure strongly discourages you from writing anything else: hence you can't mess with mutable solutions because it's easier. The resulting code is generally concise and easier to understand and debug.
James (Compojure): This is a difficult question to answer, because Clojure's features tend to be designed to solve very specific problems. For example, protocols provide polymorphism, but not inheritance or encapsulation. As such, it's difficult to pick out a single feature that I'd consider to be most useful, because it's the combination, the concert of all these individual tools that makes Clojure such a joy to work with. So I'm going to cheat a little in my answer, and say that it's this emphasis on simplicity that I find most useful. In Clojure I can choose only the specific tools I need to solve the problem, whilst in many other languages I'd need to work within a particular framework that might not always allow for an optimal solution.
InfoQ: Would you like to explain to us how your project works and how developers can use it?
Christophe (Enlive): Enlive is a HTML manipulation library. There are two main use-cases: webscraping and templating. With Enlive, templates are based on plain HTML files, potentially straight from the designers with no special markup, no convention ; in Clojure you describe how to transform this HTML to generate the actual output (which parts to repeat, where to put data etc.), selectors closely modeled after CSS3 are used to identify the places of such transformations. It makes really easy to roundtrip the design.
Similarly, when scraping, selectors are used to denote the pieces of data to retrieve.Technically put, Enlive serves the same goals as XSLT but you write Clojure code instead of XML and CSS-like selectors are used in lieu of XPath.
James (Compojure): Compojure is a small web framework based on Ring, and provides a concise routing DSL that developers can use to define web applications. Usually Compojure is used in conjunction with other libraries, such as Hiccup or Enlive for templating, or ClojureQL for accessing the database.
InfoQ: What tools do you use for building Clojure apps? Is there one that you find particularly useful?
Christophe (Enlive): I use CCW (Counterclockwise) the Eclipse plugin for Clojure, it's making great progress and its main developer is really commited to it.
James (Compojure): I use Emacs with SLIME for developing, and Leiningen for building and deploying. When you get used to it, the Emacs paredit mode is very useful for quickly moving around S-expressions. I also tend to keep a SLIME session running and reload my source code often, so I'm always developing against a running environment.
InfoQ: What is the future roadmap for your project?
Christophe (Enlive): Moving it to clojure contrib and after that I'd like to make its behavior more tunable in regards to caching, encoding and escaping.
James (Compojure): I actually don't plan on adding much more to Compojure, at least not for a while.
I tend to prefer small libraries and functions that perform specific tasks, rather than large frameworks that attempt to do everything. There's little I can add to Compojure that doesn't seem more suitable in a separate library.
For example, one project idea I've been meaning to get around to writing is an equivalent of the respond_to method included in Ruby on Rails, which allows developers to specify HTTP responses in different formats. I could include this as part of Compojure, but there's no reason not to implement it as a separate library, and not limit it to a particular framework.
Todd Montgomery Dec 19, 2014