Literature Programming for Clojure with Babel and Spacemacs

in #programming7 years ago

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

Sort:  

From 2013-2016 I've created several largish literate programming projects in Clojure entirely in org-mode (e.g. thi.ng/geom, thi.ng/fabric) and created a Leiningen project template to simplify the creation of new LP projects...

After several years working this way I generally still believe the LP approach of formulating the actual problem in prose form before/during coding has allowed me to produce better code and work on more projects in parallel than writing code "normally". OTOH I'm still very much on the fence about how suitable this way of working is for open source projects. The by far largest (and unrefutable) criticism is that one essentially forces any contributor to use emacs and that simply doesn't work. Also, now that my professional focus has shifted away from Clojure, that means alot of these projects slowly become unmaintained largely because there's this huge barrier of entry just from the tooling side... In another life I might still be tempted to work on more modern/approachable LP solution myself :)

Hey cryptothing! I've come across your stuff, and returned to it a few times (understanding things slightly more each time), albeit not enough to properly utilize it. I'm really a musician/producer, but started dabbling in coding, mainly Clojure, a couple of years back. Through this I got into emacs, with CIDER and org-mode (and I can't understand why other people wouldn't want to use emacs, particular with the likes of spacemacs catering for vim users). I can totally see the benefits of using the literal programming method, and how simple org-mode makes this sort of working, and how much it could vastly speed up the development of projects.

You libraries gush quality! I imagined somehow using geom to create some kind of audio-reactive beast. I'll be resuming my efforts shortly, and hopefully will be a bit more focused in the future (thanks to shedding some unproductive habits).

Interesting, would love to see how structuring a larger code base be like using LP. Also, why did you leave clojure?

Coin Marketplace

STEEM 0.16
TRX 0.12
JST 0.026
BTC 56905.21
ETH 2508.41
USDT 1.00
SBD 2.36