开发者

How can you dump contents of Clojure REPL to a file?

So I have been working on a Clojure tutorial and it's pretty fun so far. 开发者_Go百科Unfortunately, every time I close my REPL out, I lose all of the defn and def that I created in the previous session.

So, to save time in the future, is it possible to have the Clojure REPL save everything I've typed to a file so I can dig out what I need for future uses?


I think most people work by using their IDE to send fragments of code to the REPL, rather than doing a lot of direct hacking in the REPL. This way you have a better idea of the current state of the program.


If you use Emacs and SLIME, you can save the REPL buffer to a file just like saving any other buffer.


If you are using a Unix-like operating system you can start your REPL using rlwrap. This would give you Ctrl-R for accessing commands from history in an easy way, as well as storing the history itself.

You'd just have to install rlwrap and prepend it to the line where you start REPL, e.g. rlwrap lein repl


Couple of tips:

  • I do a lot of coding in the REPL, but as soon as I write something that works then I copy/paste it into a source file. Then I can just run or reload that source file whenever I want to get a REPL environment back to a fully configured state.
  • Some REPLs (e.g. the Eclipse Counterclockwise REPL that I use) keep history from previous sessions so you can up-arrow to the earlier REPL commands you want to repeat.


This is actually a harder problem than it seems because a dump like this must take into account symbol and vars dependencies and output to file in the correct order of symbols. That said since code is data one should be able to write a function that does that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜