Tech behind Tech

Raw information. No finesse :)

Clojure MindMap

with 4 comments

I created this when I was getting started with Clojure. Sharing as others might find it useful.

Written by Siva Jagadeesan

September 24, 2012 at 5:00 am

Posted in Clojure

Tagged with

Introduction to Clojure Web Development using Ring, Compojure and Sandbar

with 14 comments

I gave an introduction to clojure web development presentation to bay area clojure user group. As my slides do not have much information, I am writing this blog so everyone can follow and get started with a sample clojure web application using compojure.

Setting Up:

 lein new address_book 

Add Compojure to Project:

Edit project.clj

(defproject address_book "1.0.0-SNAPSHOT"
  :description "Runnering log"
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]
                 [ring/ring-jetty-adapter "0.2.5"]
                 [compojure "0.4.1"]
                 [hiccup "0.2.6"]
                 [sandbar "0.3.0-SNAPSHOT"]
                 [clj-json "0.3.1"]]
  :dev-dependencies [[swank-clojure "1.3.0-SNAPSHOT"]])


lein deps

Test whether our setup works:

Edit src/address_book/core.clj

(ns address-book.core
  (:use [compojure.core]
  (:require [compojure.route :as route]))

(defroutes rts
  (GET "/" [] "Address Book!!")
  (route/not-found "Page not found"))

(def application-routes

(defn start []
  (run-jetty application-routes {:port 8080
                                 :join? false}))

Goto http://localhost:8080 and you should see “Address Book”

Interactive Development:

In core.clj, change start function to

(defn start []
  (run-jetty application-routes {:port 8080
                                 :join? false}))

Add Static Folders:

mkdir -p public/{css,js}

Create public/index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"	"">'
<html xmlns="" xml:lang="en">	
   <meta http-equiv="content-type" content="text/html; charset=utf-8" />		
   <title>My Address book</title>		
   <script type="text/javascript" src=""></script>	
 <body id="main">    
    <p>      My Address Book    </p>  

Try to access http://localhost:8080/index.html. This will fail, as we never told compojure how to handle static files.

Edit core.clj and replace rts

(defroutes rts
  (GET "/" [] "Address Book!!")
   (route/files "/")
  (route/not-found "Page not found"))

Now when you access http://localhost:8080/index.html, it should display correctly.


Three components

1) Handlers:
Handlers are main functions that process a request. We define handlers using defroutes macro.

2) Middleware:
Middleware are functions that could be chained together to process a request. Middleware functions can take any number of arguments, but the spec stats that first argument should be an handler and function should return an handler. An example for middleware is logging all requests that comes to your webserver. Ring and compojure comes with some standard middleware. We will see in next part how to create our own middleware.

3) Adapters:
Adapters are functions could adapt our handler to a web server. We are using jetty adapter to tie our handler to jetty server.

Lets create our Middleware:

Create src/address_book/middleware.clj

(ns address-book.middleware)

(defn- log [msg & vals]
  (let [line (apply format msg vals)]
    (locking System/out (println line))))

(defn wrap-request-logging [handler]
  (fn [{:keys [request-method uri] :as req}]
    (let [resp (handler req)]
      (log "Processing %s %s" request-method uri)

Edit core.clj ns form

(ns address-book.core
  (:use [compojure.core]
  (:require [compojure.route :as route]
			[address-book.middleware :as mdw]))

Edit core.clj application-routes def

(def application-routes
     (-> rts

Now when you access your server, you should see request log messages.

Main sourcecode:

Create address_book/address.clj

(ns address-book.address
  (:import [java.util Date])
  (:refer-clojure :exclude (find create)))

(def STORE (atom {:1 {:id :1 :name "Siva Jagadeesan" :street1 "88 7th" :street2 "#203" :city "Cupertino" :country "USA" :zipsourcecode 98802}}))

(defn to-keyword [num]
  (if-not (keyword? num)
    (keyword (str num))

(defn random-number []
  (to-keyword (.getTime (Date.))))

(defn create [attrs]
  (let [id (random-number)
        new-attrs (merge {:id id} attrs)]
    (swap! STORE merge {id new-attrs})

(defn find-all []
  (vals @STORE))

(defn find [id]
  ((to-keyword id) @STORE))

(defn update [id attrs]
  (let [updated-attrs (merge (find id) attrs)]
    (swap! STORE assoc id updated-attrs)

(defn delete [id]
  (let [old-attrs (find id)]
    (swap! STORE dissoc id)

Edit core.clj ns form

(ns address-book.core
  (:use [compojure.core]
  (:require [compojure.route :as route]
			[address-book.middleware :as mdw]
            [address-book.address :as address]
            [clj-json.core :as json]))

Add this function to core.clj

(defn json-response [data & [status]]
  {:status (or status 200)
   :headers {"Content-Type" "application/json"}
   :body (json/generate-string data)})

Add these routes to core.clj

(GET "/addresses" [] (json-response (address/find-all)))
(GET "/addresses/:id" [id] (json-response (address/find id)))
(POST "/addresses" {params :params}  (json-response (address/create params)))

Replace your public folder with

This folder has needed css and js files.

Now go to http://localhost:8080/index.html and you should see a address book webapp. You can add and view addresses.


Create src/address_book/auth.clj

(ns address-book.auth
  (:use [sandbar.form-authentication ]

(defrecord AuthAdapter []
  (load-user [this username password]
             (cond (= username "example")
                   {:username "example" :password "password" :roles #{:user}}))
  (validate-password [this]
                     (fn [m]
                       (if (= (:password m) "password")
                         (add-validation-error m "Unable to authenticate user.")))))

(defn form-authentication-adapter []
   {:username "Username"
    :password "Password"
    :username-validation-error "You must supply a valid username."
    :password-validation-error "You must supply a password."
    :logout-page "/"}))

Edit core.clj ns form

(ns address-book.core
  (:use [compojure.core]
        [sandbar.form-authentication ]
        [sandbar.validation ])
  (:require [compojure.route :as route]
			[address-book.middleware :as mdw]
            [address-book.address :as address]
            [address-book.auth :as auth]
            [clj-json.core :as json]))

Add this def to core.clj

(def security-policy
  [#".*\.(css|js|png|jpg|gif|ico)$" :any
   #"/login.*" :any
   #"/logout.*" :any
   #"/permission-denied.*" :any
   #"/addresses" :user
   #"/index.html" :user
   #"/" #{:user}])

Add this route to core.clj

 (form-authentication-routes (fn [_ c] (html c)) (auth/form-authentication-adapter))

Change application routes function in core.clj

(def application-routes
     (-> rts
        (with-security security-policy form-authentication)  		 

Now when you access http://localhost:8080/index.html it will take you to login page. You can login using “example” and “password”.

That is it folks. A simple web app using clojure.

I am sure this blog could be improved a lot. Please leave comments I will update this blog with your feedback.

Written by Siva Jagadeesan

January 19, 2011 at 12:36 am

Posted in Clojure

Tagged with , , , ,

Leiningen – Adding git submodule to Leiningen project

with 4 comments

With git, sometimes it easy to use git submodule for dependency management. In Leiningen, for some reason the only “advertised” way of dependency management is using maven. After some poking around, there is a way in Leiningen to use git submodules for dependency management.

Are you ready? ( drumroll …)

It is very simple actually :)

You have to checkout your submodules to “checkouts” folder. Leiningen will automatically add your submodule’s src directory to your classpath.

That’s it folks :)

Written by Siva Jagadeesan

December 11, 2010 at 1:39 pm

Posted in Clojure

Tagged with , ,

Clojure – good coding guidelines

with 6 comments

Naming :

– only lower case letters separated by hyphens except types and protocols should be named in camelcase
– predicates ends with ?
– destructive functions ends with !
– variables that are meant for be re-binding should have earmuffs
– use “_” for names that will be ignored by the code

Code Structure :

– when there is only “then” clause in a conditional statement use “when” instead of “if”
– put all trailing parenthesis in one line
– for dependency use “:only” for “use” and “require”
– use two spaces for indentation
– destructure arg list only when you want to explain the structure of arg to caller otherwise destructure in first let binding

Coding Style:

– try to solve a problem by using function before trying with macro
– be lazy when possible
– use keywords as keys for map entries
– use keyword-first syntax to access properties on objects
– prefer sequence library functions instead of loop/recurr
– only use anonymous functions for short function definitions that fit comfortably on a single line. Otherwise create a private, named function.
– short functions

Written by Siva Jagadeesan

December 9, 2010 at 11:04 pm

Posted in Clojure

Tagged with ,

Clojure defrecord deftype

with 2 comments

There are different ways to create a struct in clojure.

1) Using Clojure built in data structures

user> (def lotr {:title "Book title"
                 :author {
                          :first-name "first"
                          :last-name "last"}})


user> (:title lotr)
"Book title"


user> (-> lotr :author :first-name)

2) Using defrecord

user> (defrecord Book [title author])
user> (defrecord Person [first-name last-name])
user> (def lotr (Book. "Book Title"
                       (Person. "first" "last")))


user> (:title lotr)
"Book title"


user> (-> lotr :author :first-name)

As you can see, accessing map and defrecord is very similar. defrecord provides implementation of a persistent map.

3) Using deftype

PS: bad example ( I will explain why later )

user> (deftype Book [title author])
user> (deftype Person [first-name last-name])
user> (def lotr (Book. "Book Title"
                       (Person. "first" "last")))

Unlike defrecord, deftype does not provide implementation of a persistent map. So when you try to keyword-access , it won’t work


user> (:title lotr)

It provides field access only

user> (.title lotr)
"Book Title"

I mentioned the above example for deftype is a bad example. Let me explain why. In OO, we have two set of classes. One set that defines language constructs like String and other that defines domain like Book. As defrecord provides implementation of a persistent map, it is better to use defrecord for realizing domain. Use deftype for realizing programming constructs.

Written by Siva Jagadeesan

November 15, 2010 at 10:58 am

Posted in Clojure

Tagged with , , , ,

Testing Clojure Code – Awesome “are”

with 2 comments

test-is a great library for testing clojure. Thanks to Philippe, I recently discover “are” in test-is. This is most underrated/underused macro in test-is library.

Lets take an example, where I want to test a function given a date, it will return end of month.

Without are,

(deftest test-end-of-month
        (is (= "2010-01-31 23:59" (end-of-month "2010-01-03 01:12")))
        (is (= "2010-01-31 23:59" (end-of-month "2010-01-03 21:59")))
        (is (= "2010-02-28 23:59" (end-of-month "2011-02-03 13:01")))
        (is (= "2016-02-29 23:59" (end-of-month "2016-02-03 03:01")))
        (is (= "2011-04-30 23:59" (end-of-month "2011-04-03 00:02"))))

With are,

(deftest test-end-of-month
        (are [expected ts] (= expected (end-of-month ts))

             "2010-01-31 23:59" "2010-01-03 01:12"
             "2010-01-31 23:59" "2010-01-03 21:59"
             "2011-02-28 23:59" "2011-02-03 13:01"
             "2016-02-29 23:59" "2016-02-03 03:01"
             "2011-04-30 23:59" "2011-04-03 00:02"))

The version without “are” has, so many duplications. It is difficult to read as it is cluttered. The version with “are” is easy to read, and it is so easy to add more test cases without much duplication.

Thanks Philippe for introducing me to wonders of “are”.

Written by Siva Jagadeesan

October 19, 2010 at 12:55 pm

Posted in Clojure

Tagged with , ,

Calling java from Clojure

leave a comment »


Importing Java Class

In repl, when you want to import one Java class you can do

(import 'java.util.Date)

When you want to import more Java classes from a same package you can do

(import [java.util Date HashMap])
        (:import [java.util Date HashMap]))

Creating Instances

(import 'java.util.Date)

(def today (new Date))

;; or

(def today (Date.))

Calling Java instance methods

user> (import 'java.util.Date)
user> (let [today (Date.)]
        (.getTime today))

Calling Java static methods

user> (System/currentTimeMillis)

Sugar sytax:


You want to write a function that will return UTC Java Calendar object set at a specific time.

user> (import [java.util Calendar TimeZone Date])
user> (defn utc-time [d]
        (let [cal (Calendar/getInstance)]
              (.setTimeZone cal (TimeZone/getTimeZone "UTC"))
              (.setTime cal d)


The let block is ugly. We could use doto to make this code better.

user> (import [java.util Calendar TimeZone Date])	
user> (defn utc-time [d]        
         (doto (Calendar/getInstance)
              (.setTimeZone (TimeZone/getTimeZone "UTC"))
              (.setTime d)))


Dot Dot:

Sometimes in java you want to make calls in chain

Bad way

user> (.length (.getProperty (System/getProperties) ""))

Better way

user> (. (. (System/getProperties) getProperty "") length)

Even Better

user> (..
       (getProperty "")

Avoiding Reflection

By default jvm will be using reflection to identify type. Reflection is slow. But we can give type hints that way jvm does not have to use reflection. For example we can rewrite utc-time with type-hints like this

user> (set! *warn-on-reflection* true)
user> (defn str-length [s] (.length s))
Reflection warning, NO_SOURCE_FILE:1 - reference to field length can't be resolved.
user> (defn str-length [#^String s] (.length s))

Implementing interfaces and extending classes

Let us implement Java Runnable interface

user> (proxy [Runnable] []
        (run []
             (println "running ...")))
#<Object$Runnable$36fc6471 user.proxy$java.lang.Object$Runnable$36fc6471@6dd33544>

In clojure 1.2, you could use reify macro to implement. In fact it is better than using proxy.

user> (reify Runnable
             (run [this]
                  (println "running ...")))
#<user$eval1664$reify__1665 user$eval1664$reify__1665@574f7121>

Written by Siva Jagadeesan

October 11, 2010 at 10:43 pm

Posted in Clojure

Tagged with , ,


Get every new post delivered to your Inbox.

Join 147 other followers

%d bloggers like this: