This post is from a low-reputation account and contains an unverified outbound link. Be cautious before clicking external links.

Literature Programming for Clojure with Babel and Spacemacs

Words
436
Reading
2 min
Listen
Play
9y

I've learned about literature programming years ago when I was studying for my Computer Science degree. Basically it is a way of programming to interwind narrative text and executable code, such that the order of the text is aimed at narrative purpose first, code execution logic second. Wherein traditional programming, often the case is that narrative comments complies to the order of the code blocks, making source code reading an unpleasent experience.

Back in those days, I recall using something call "Noweb" to compile or generate the source once the file is done. A pdf and the related source code will then be produced. I used VIM, but the editing experience was pretty bad that I don't feel like I am actually letting my "flow of thoughts" generating executable code naturely. I ditched the whole idea of literature programming since then, and use something like Javadocs and the like for my documentation needs.

Babel isn't a such thing. With Emacs REPL workflow empowered, literature programming with Babel became a fluent thing – almost like writting a blog post. Then Spacemacs makes Emacs easier to use. Finally there is Clojure, a function programming language designed for REPL driven development while being a LISP, perfect match for Emacs.

Clojure project creation

To create a Clojure project we need Leiningen. The Babel provided the Shell code for this. Enter <s and tab will generate the easy template for you.

lein new babel-clj

Generating a project called babel-clj based on the 'default' template.
The default template is intended for library projects not applications.
To see other templates (app plugin etc) try `lein help new`.

Wait a second, where did I generated my project?

pwd

Wrong place. What do I do? :dir is what we need.

lein new app babel-clj

Let's start coding.

Clojure main function

:tangle can make the file generated. First we need cider-jack-in to start the repl. Let's start from fresh. ,, is the key to eval a code block. org-babel-tangle will export the file.

(ns babel-clj.core
  (:gen-class))

An awesome Clojure function.

(defn helloworld
  ([] (helloworld "No one"))
  ([someone] (str "Hello " someone)))

And now the main function.

(defn -main []
  (println (helloworld "GzMask")))

Finally let's run the whole thing from shell.

pwd

lein run

Conclusion

Now let's get this code up to github first. First this org file should be inside the code project.

cp ./Babel-clojure.org ~/sandbox/babel-clj

And then git push the thing.

git init
git add -A
git commit -am"first commit"
git remote add origin [email protected]:gzmask/Babel-clojure.git
git push -u origin master

Spacemace fix:

The spacemacs org layer is broken for babel in Clojure, apply following fix:
goto ~/.emacs.d./epla/org-plus-contrib-*/ob.clojure.el
apply fix

Literature Programming for Clojure with Babel and Spacemacs | Ecency