问题
So I have been working on a Clojure tutorial and it's pretty fun so far. 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?
回答1:
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.
回答2:
If you use Emacs and SLIME, you can save the REPL buffer to a file just like saving any other buffer.
回答3:
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
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/6692102/how-can-you-dump-contents-of-clojure-repl-to-a-file