Clojure

1 minute read

For a side project I started using Clojure. This is an experiment in functional programming with a Lisp syntax. The great thing about clojure is that it runs on the Java Virtual Machine, meaning you can have access to the full suite of Java libraries out there while not having to actually program Java.

For now I wanted to share the configuration of my development environment and I want to show some of the more interesting bits of Clojure, namely ClojureScript.

Development environment

I use Emacs as my development environment. You can find my configuration files on github.

There are several packages necesary; install with M-x packages-install [name of package]:

  • Paredit: an extremely usefull library to work with Lisp datastructures. A useful guide to this package was written by Dan Midwood.
  • Cider: CIDER is a Clojure IDE and REPL for Emacs
  • clj-refactor: A collection of simple Clojure refactoring functions for Emacs

Enabling modules

The main Cider module is easily configured. You basically just require it and you are a go!

(require 'cider)

(add-hook 'cider-mode-hook 'cider-turn-on-eldoc-mode)
(add-hook 'clojure-mode-hook 'rainbow-delimiters-mode)
(add-hook 'clojure-mode-hook 'rainbow-identifiers-mode)
(add-hook 'clojure-mode-hook 'paredit-mode)

(setq nrepl-hide-special-buffers t)

The same goes for the refactor module, but it has room to insert custom keybindings if you require them.

(require 'clj-refactor)
(add-hook 'clojure-mode-hook (lambda ()
                               (clj-refactor-mode 1)
                               ;; insert keybinding setup here
                               ))

ClojureScript and figwheel

One of the coolest things I have seen in the Clojure space up to now is the use of Clojure to compile down to Javascript. This version is called ClojureScript and is basically equivalent to Clojure, but targetted to the Javascript language.

To get a sence of the power of ClojureScript and a plugin called figwheel take a look at the following video. The author is hacking away at a flappy bird clone, while the code is being hot swapped inside the browser. No more browser reloads to get simple changes pushed to it!