In Clojure We Trust

A piece of intertube about the Clojure programming language, algorithms and Emacs.

Duplicating S-expressions on a Line

Duplicating a line is extremely useful when editing text and specially when programming. There is no native command in Emacs to achieve that but it’s easy to add one.

It’s easy to get addicted to the use of this new command but a problem remains when programming in Lisp with paredit.el: duplicating sometimes lead to invalid s-expressions being inserted.

I decided to give it a try and made an implementation that duplicate the s-expressions after the point (cursor). For instance in Clojure if you are editing this code:

(ns myns
(:use [clojure.string :only [escape]]))
You can duplicate the first vector by placing the cursor on the square bracket, invoking the command with one keystroke and then easily obtain this code:
(ns myns
(:use [clojure.string :only [escape]]
[clojure.set :only [union]]))
Here is the code to duplicate the line; to my knowledge there is no such command in paredit.el:

Not really pretty but it does the work, feel free to provide a nicer implementation in the comments or add it to your ~/.emacs.d/init.el file with:

(eval-after-load "paredit"
'(progn (define-key paredit-mode-map (kbd "C-S-d") 'paredit-duplicate-after-point)))
Edit: Here a fix for when the sexp is the last expression in the buffer.


Neale Swinnerton
Nice idea. Have you tried expand-region ? It doesn't do quite what you're trying here, but it handles most of the 'sexp that straddles a line ending issues'

An Emacs Minor Mode for the ClojureScript Compilation

When programming with ClojureScript, one has to lunch “lein cljsbuild auto” to automatically compiles its ClojureScript sources. It is convenient but a problem remains: the output of the compilation process must be manually read to see if any errors or warnings occur.

To solve this problem I programmed a simple minor mode for Emacs. If you start the cljsbuild command in a terminal buffer, it will watch the ouput and popup the window if an error or a warning occurs. It can also automatically hide the process when the compilation succeed, so that you are able to concentrate on what matters, the code.

It’s available on Marmalade or Github.

Feedback and patches are welcomed!

Implementing a Lisp

To extend my knowledge and to have a better understanding of the Lisp’s foundations, I implemented a simple Lisp in C. It supports symbols, integers, a few primitives, anonymous functions, closures, macros and garbage collection. The implementation follows the KISS principles and is around 1400 lines of code.



Hey, cool project. I just started something similar, here: . I'm intrigued by your implementation of garbage collection… an area I punted on (just using Boehm's GC), but eventually would like to implement myself. I'll be reading through your code for inspiration and comparison, so far I like what I'm seeing. You can email me (steveth45 at the google mail service).

Emacs Key Bindings for Lisp Programming

I finished reading Learning GNU Emacs, Third Editionthis week. The edition is from 2004 but the book has aged well. All essential concepts, modes and key bindings are presented. The Emacs documentation system is also explained, which is useful to know if you want to further extend your knowledge once you read the book. One chapter is an introduction to Emacs programming with Emacs Lisp. It gives the basis to start and explains how to implement a simple major mode. Of course since 2004, the Emacs ecosystem has changed so you won’t find something on Magit mode (for Git) or on the Emacs Lisp Package Archive but if you are not an expert Emacs user, you will learn from reading it.

One chapter is on programming modes and Lisp key bindings are presented. Even if you don’t use paredit (and really you should - just take a few minutes to learn its key binding) many commands are available to work with S-expressions. Here is one table of the book that illustrates them:

Structurally editing S-expressions gives an intense satisfaction and will make you regret Lisp syntax whenever you have to program with an another language family.

Js2-mode Fun

In Emacs with the JavaScript js2-mode…

No side effects? How bad can THAT be :-) ?


Konrad Hinsen
A function called send_data that has no side effects is a bit suspect.
Roman Parykin
Code without side effects?
It's the ugliest thing one could do in js… :)

Updating Clojure Namespaces

Continuing my experimentations with Emacs Lisp, I created a function that automatically updates the namespace of a Clojure buffer in conformity with its pathname. This is useful after renaming a file, since changing a file pathname requires to change its namespace, which is sometimes annoying to do by hand.
Once added to your .emacs file, you can call it with:
M-x clojure-update-ns

It assumes clojure-mode is already loaded.


Thanks! An improved version of this function, called clojure-update-ns, has been integrated into the latest clojure-mode.el, available at
Great little function - thank you! Next step would be to create one that recursively traverses a directory applies this to every .clj file found :)

Earmuffs and Variables

According to the Clojure coding standards for libraries, variables names should be written with *earmuffs* only when they are intended to be rebind.

Here and there I forget this rule and use earmuffs most of the time, so I decided to create an Emacs Lisp function to add/remove earmuffs to variable:

Add it to your .emacs file and then place your cursor on a variable and call it with
M-x earmuffy
to add earmuffs, or type
C-u M-x earmuffy
to remove them.


Thanks for providing a better implementation.

I'm experimenting with Emacs Lisp time to time but didn't read so much documentation about it yet so it's welcome to see improvement to my experiments.
I bit the bullet and implemented this:
Wouldn't it be nicer to have toggle-earmuffs, which detects what you currently have and toggle it?

Dear Language Designers, Do Not Forget Ada

These last days I took some time to refresh my old memories of the Ada language. While Ada, like all languages, has defaults on its own I’m still very impressed by some functionalities it offers and that current mainstream languages still don’t have. Most of these functionalities are there since the first standardized version in 1983 (since then the language standard was extended in 1995, 2005 and the next version is expected for 2012).

The first impressive future is about primitive types. Here are some of the types defined in Ada and their C equivalent (taken from [1]):

Ada TypeDescriptionC Equivalent
CharacterA single characterchar
IntegerAn integer (32-bit) numberint
NaturalZero or positive integer-
PositiveA positive integer-
Long_IntegerA big integer (same as long in Gcc)long (same as int in Gcc)
Long_Long_IntegerA really big (64-bit) integerlong long
Short_IntegerA small (16-bit) integershort
Short_Short_IntegerA really small (8-bit) integerchar
FloatA real numberfloat
Long_FloatA big real numberdouble
Long_Long_FloatA really big real numberlong double
Short_FloatA smaller real number?
FixedA fixed-point real number-
StringAn Ada fixed-length stringchar array

Boolean types are also provided. New types are defined and type safety is guaranteed. Here we define a new type based on a Long_Float and a variable of this type:
type Speed is new Long_Float;

Speedy_Gonzales_Speed : Speed;

Since Speed is a new type, it is not a Long_Float and thus assignment from a Long_Float value to a Speed variable is forbidden:
X : Long_float := 300.0; 
Speedy_Gonzales_Speed := X;
The compiler effectively gives an error:

expected type "Speed" defined at line 6
found type "Standard.Long_Float"

When it makes sense, we can define a subtype of another primitive type. Assignments between types and their subtypes is allowed. Here we define the type Degree which is a subtype of Float:
subtype Degree is Long_Float;

Oven_Temperature : Degree;
Y : Long_Float := 255.0;

Oven_Temperature := Y;

Types can be constrained with a range definition (taken from [2]):
type Degrees is new Float range -273.15 .. Float'Last;

This allow a Degrees variable to range from -273.15 (absolute zero) to the last value allowed by a Float.

This strong typing provides safety and contrasts with what is seen in current mainstream programming languages. Languages like Java don’t provide rich primitive types (there are for instance no unsigned integer in Java!) and require the programmer to define its own cumbersome classes if he wants new types (which poses readability problems if the types are new number types and the language does not support operator overloading). Some languages like C don’t even bother so much about type conversion: a float can be assigned to an integer. This leads to solutions which are far from being satisfying (see for instance C FAQ - round).

A lot of other possibilities are offered to the Ada programmer, for instance decimal types can be defined with their precision. The precision is then guaranteed by the compiler:
type money is delta 0.01 digits 18;
Ada also supports multidimensional arrays, bit-level memory access, definition of memory pool, concurrency programming in a task-oriented way, object-oriented programming, generic packages etc.

At another level Scheme and Common Lisp supports a numeric towel. For example, 3 is an integer. Therefore 3 is also a rational, a real, and a complex (example from the Scheme standard).

;; in Scheme:
> (rational? (/ 1 5))
> (rational? 3)
> (real? 3)
While this discussion may not make so much sense for dynamic languages such as Clojure, I think new languages being designed, which are not dynamically typed, could really benefit of having such rich primitives types. A complex type systems like the one from ML or Haskell is not needed for this purpose and primitives types is the most basic and most used feature of a programming language, so dear languages designers, next time, have a small thought for Ada.


Fed Up With Typing Ns Declarations?

While watching the demonstration video of the Play framework, I saw that the developer had some nice templates for its TextMate editor. A few Google searches tells us that it is also possible to have the same functionality in Emacs with the YASnippet template system.

Typing all these long namespaces declarations in Clojure is quickly boring when you create a lot of files, so why not create a template for that?

After having installed YASnippet, create a clojure-mode directory inside the yasnippet/snippets/text-mode directory and create a file named ns with this content:

(ns `(let* ((nsname '())
(dirs (split-string (buffer-file-name) "/"))
(aftersrc nil))
(dolist (dir dirs)
(if aftersrc
(setq nsname (cons dir nsname))
(setq nsname (cons "." nsname)))
(when (or (string= dir "src") (string= dir "test"))
(setq aftersrc t))))
(when nsname
(replace-regexp-in-string "_" "-" (substring (apply 'concat (reverse nsname)) 0 -5))))`
(:use $1)
(:require ))

Now, when inside a Clojure buffer type “ns” and TAB to complete ; if you are for instance in the src/mylib/utils/swing_stuff.clj buffer, this will be expanded into the following text:
(ns mylib.utils.swing-stuff
(:use )
(:require ))

Isn’t that handy?

Note: this is my first hack with Emacs Lisp, and now that it is working I’m publishing it without any further improvements, so be indulgent regarding the implementation!


Max Penet
Loving it, thanks!

I didn't know it was possible to write elisp in snippets directly.

Ray Tracing, Once Again

There are already several implementations of simple raytracers in Clojure on the Web but I couldn’t resist implementing one for myself! It is based on the one of the book Ansi Common Lisp by Paul Graham.

Nothing particular, just perhaps the use of protocols for the dispatch of the normal, color and intersect functions and some code for displaying spirals.

Code is available here: GitHub Kototama