In Clojure We Trust

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

Sunday, August 11, 2013

Migrating Away From Google Blogger

This blog is moving away from Google Blogger. The new URL is http://dialectical-computing.de/blog/ Please update your RSS reader.

Tuesday, January 29, 2013

Searching in Clojure and ClojureScript files with Ack

"Ack is a tool like grep, optimized for programmers". I knew the existence of Ack since a few years but I never gave it try. Now that I did, I'm positively surprised: it's easy to use and fast.

I was always fighting in Emacs between grep (what is the syntax?), igrep-find (why did it suddenly stopped to prompt for directories?), tags-search (ooops some of my files are not tagged) and rgrep (oh it did exist?). I installed ack-and-a-half for Emacs and its great. Ack-and-half tries to find automatically the root directory of your project but you can set ack-and-a-half-prompt-for-directory to true with (setq ack-and-a-half-prompt-for-directory t) for it to ask which directory to search in.

The Ack version on the website provides support for Clojure files. For ClojureScript files, you need to add the following lines to your ~/.ackrc file:

--type-add
clojure=.cljs

Et voilĂ , enjoy!

Tuesday, January 15, 2013

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.

Thursday, October 18, 2012

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!

Wednesday, May 2, 2012

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.


Discussion: http://news.ycombinator.com/item?id=3919685
Code: https://github.com/kototama/kml

Thursday, February 23, 2012

Emacs key bindings for Lisp programming

I finished reading Learning GNU Emacs, Third Edition this 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.

Tuesday, February 14, 2012

js2-mode fun


In Emacs with the JavaScript js2-mode...



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



Followers