Intro to Clojure on the Web

Lisp is one of those languages that people either love or hate. Count me among the Lisp lovers. I was brainwashed during my undergraduate studies at MIT to believe that Lisp is the only "real" programming language out there, and that anything else is a pale imitation. True, I use Python and Ruby in my day-to-day work, but I often wish I had the chance to work with Lisp on a regular basis.

One window of opportunity to do exactly that has opened in the past few years. Clojure, a modern variant of Lisp that runs on the Java virtual machine (JVM), has been taking the programming world by storm. It's a real Lisp, which means that it has all of the goodness you would want and expect: functional programming paradigms, easy use of complex data structures and even such advanced facilities as macros. Unlike other Lisps, and contributing in no small part to its success, Clojure sits on top of the JVM, meaning that it can interoperate with Java objects and also work in many existing environments.

In this article, I want to share some of my experiences with starting to experiment with Clojure for Web development. Although I don't foresee using Clojure in much of my professional work, I do believe it's useful and important always to be trying new languages, frameworks and paradigms. Clojure combines Lisp and the JVM in just the right quantities to make it somewhat mainstream, which makes it more interesting than just a cool language that no one is really using for anything practical.

Clojure Basics

Clojure, as I mentioned above, is a version of Lisp that's based on the JVM. This means if you're going to run Clojure programs, you're also going to need a copy of Java. Fortunately, that's not much of an issue nowadays, given Java's popularity. Clojure itself comes as a Java archive (JAR) file, which you then can execute.

But, given the number of Clojure packages and libraries you'll likely want to use, you would be better off using Leiningen, a package manager for installing Clojure and Clojure-related packages. (The name is from a story, "Leiningen and the Ants", and is an indication of how the Clojure community doesn't want to use the established dependency-management system, Ant.) You definitely will want to install Leiningen. If your Linux distribution doesn't include a modern copy already, you can download the shell script from https://raw.github.com/technomancy/leiningen/stable/bin/lein.

Execute this shell script, putting it in your PATH. After you download the Leiningen jarfile, it will download and install Leiningen in your ~/.lein directory (also known as LEIN_HOME). That's all you need in order to start creating a Clojure Web application.

With Leiningen installed, you can create a Web application. But in order to do that, you'll need to decide which framework to use. Typically, you create a new Clojure project with lein new, either naming the project on which you want to work (lein new myproject), or by naming the template you wish to copy and then the name of the project (lein new mytemplate myproject). You can get a list of existing templates by executing lein help new or by looking at the https://clojars.org site, a repository for Clojure jarfiles and libraries.

You also can open an REPL (read-eval-print loop) in order to communicate directly with Clojure. I'm not going to go into all the details here, but Clojure supports all the basic data types you would expect, some of which are mapped to Java classes. Clojure supports integers and strings, lists and vectors, maps (that is, dictionaries or hashes) and sets. And like all Lisps, Clojure indicates that you want to evaluate (that is, run) code by putting it inside parentheses and putting the function name first. Thus, you can say:


(println "Hello")
(println (str "Hello," " " "Reuven"))
(println (str (+ 3 5)))

You also can assign variables in Clojure. One of the important things to know about Clojure is that all data is immutable. This is somewhat familiar territory for Python programmers, who are used to having some immutable data types (for example, strings and tuples) in the language. In Clojure, all data is immutable, which means that in order to "change" a string, list, vector or any other data type, you really must reassign the same variable to a new piece of data. For example:


user=> (def person "Reuven")
#'user/person

user=> (def person (str person " Lerner"))
#'user/person

user=> person
"Reuven Lerner"

Although it might seem strange to have all data be immutable, this tends to reduce or remove a large number of concurrency problems. It also is surprisingly natural to work with given the number of functions in Clojure that transform existing data and the ability to use "def" to define things.

You also can create maps, which are Clojure's implementation of hashes or dictionaries:


user=> (def m {:a 1 :b 2})
#'user/m

user=> (get m :a)
1

user=> (get m :x)
nil

You can get the value associated with the key "x", or a default if you prefer not to get nil back:


user=> (get m :x "None")
"None"

Remember, you can't change your map, because data in Clojure is immutable. However, you can add it to another map, the values of which will override yours:


user=> (assoc m :a 100)
{:a 100, :b 2}

One final thing I should point out before diving in is that you can (of course) create functions in Clojure. You can create an anonymous function with fn:


(fn [first second] (+ first second))

The above defines a new function that takes two parameters and adds them together. However, it's usually nice to put these into a named function, which you can do with def:


user=> (def add (fn [first second] (+ first second)))
#'user/add

user=> (add 5 3)
8

Because this is common, you also can use the defn macro, which combines def and fn together:


user=> (add 5 3)
8

user=> (defn  add [first second] (+ first second))
#'user/add

user=> (add 5 3)
8

______________________

Reuven M. Lerner, Linux Journal Senior Columnist, a longtime Web developer, consultant and trainer, is completing his PhD in learning sciences at Northwestern University.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

LISP

Callen's picture

very interesting topic for Lisp programming language. It is very useful part to anyone.thanks for sharing this valuable information. http://nopalea.jigsy.com/nopalea

Lisp programming language

Jony's picture

I think Lisp programming language growing popular day by day. Also Clojure for Web development sector Lisp programming language is important. Thanks to introduce me on Clojure Basics. http://www.healthforus.info

The maintenance cost

sollen's picture

The maintenance cost estimates are based on the cost to maintain 50W 24V LED Fire Engine Work lamps a vehicle and perform needed repairs for five years and 75,000 miles, including labor expenses, replacement part prices and the purchase of an extended warranty.

LISP very rarely used....

mishainfotech's picture

I agree with you. Now a days, we only use the LISP in college days not in real life as it is rarely used and new technologies and advancements are used in real practice.

++++

cnbestmall's picture

++++ http://www.cnbestmall.com ++++

Accept Paypal and Credit Card, FREE SHIPPING

Nike AIr max, Shox, Rift, dunk, blazer, air force 1 shoes: 48 USD

Nike free running shoes: 42 USD

D&G, LV, Gucci, parda DC, polo, puma, supra shoes: 42 USD.

Timberland boot: 50 USD

T-shirts (polo, ed hardy, lacoste,gucci, lv, etc) $28

Jeans (AF, armani, bape, BBC, CA, coogi, D&G, Diesel, Evisu, Levis, gucci, true religion, versace) 45 USD

Down Coat jacket parka vests (moncler, canada goose, barbour, parajumpers, woolrich) 168 USD-268 USD

++++ http://www.cnbestmall.com ++++

Well, OK

Anonymous's picture

If you like LISP then I guess clojure is fine for you.

Frankly, if I were programming functionally I'd be using either erlang or haskell. LISP is over 50 years old and looks its age.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState