2

I'm currently learning Clojure, and I'm evaluating sample code from the book in begin_src/end_src blocks. Works quite well, as org adds structure to otherwise unrelated pieces of code.

The problem starts when these pieces become related, i.e. sample code for chapter on agents uses a function defined for refs two chapters back. So on attempt of evaluating the last block from a new nrepl session, an undefined function error is thrown. Then I have to find the block where the relevant function is actually defined and evaluate that. And I'm lucky if it doesn't depend on anything else.

One solution could be to re-evaluate sequentially all the source blocks. That would certainly work, but a very large number of blocks would be evaluated needlessly, as they're not among the dependencies of the block that I actually want to evaluate.

I'm thinking that perhaps some mechanism akin to auto loading could be implemented, e.g.:

#+begin_src clojure :defines square
(defn square [x]
  (* x x))
#+end_src

So that when I'm evaluating this block:

#+begin_src clojure
(square 5)
#+end_src

and "Unable to resolve symbol: square" appears, org will try to find if any of the source blocks in current buffer define this function.

I'm open to any other suggestions to solve this problem, as long as the solution allows me to effectively store the code samples from the whole book in a single org file.

Alex Miller
  • 69,183
  • 25
  • 122
  • 167
abo-abo
  • 20,038
  • 3
  • 50
  • 71
  • I think `org-babel` refers to this as "sessions", but not all languages support them, you'd need to check if Clojure module does. (AFAIK, Java doesn't for example). http://orgmode.org/worg/org-contrib/babel/intro.html#sec-5-2 here it describes it. –  Sep 27 '13 at 14:10
  • The session works fine for Clojure. The point is that I've got ~100 source blocks in a file. And I don't know yet how to evaluate them all at once (and eventually only the ones that are the dependencies). – abo-abo Sep 27 '13 at 14:15
  • I'm not aware of a general-case solution, but I'd imagine that Clojure-specific code would be in a `ob-clojure.el`, it shouldn't be a lot of code, so, maybe you could start reading there and see if you can add more keywords to trigger your own code that would manage evaluation of dependencies. I'd also look into `:noweb` (literate programming) because it somehow knows how to assemble larger programs from separate blocks of code. –  Sep 27 '13 at 14:36
  • Are you trying to evaluate them while working? If you use session and evaluate on export it should provide the correct results as you work through the document. – Jonathan Leech-Pepin Oct 04 '13 at 13:46
  • @JonathanLeech-Pepin, I have currently about 100 clojure blocks in a document. Sometimes I want to use a function in block 101, that was defined somewhere above without having to find it manually. I don't care about exporting, I'm just going through a book's examples. – abo-abo Oct 04 '13 at 14:04

0 Answers0