the inverse function of load or load-file

333 views
Skip to first unread message

ogcraft

unread,
Jun 23, 2009, 10:11:32 AM6/23/09
to Clojure
Dear all,
Is there an inverse function of load or load-file.
I mean the "save" function which writes the clojure variables or
entire workspace context (in textual form) to the file?
That we can load-file on such file and get the same variables as when
"save" was originally invoked.

Adrian Cuthbertson

unread,
Jun 23, 2009, 11:33:53 PM6/23/09
to clo...@googlegroups.com
You can use the following;

(defn frm-save
 "Save a clojure form to file."
  [#^java.io.File file form]
  (with-open [w (java.io.FileWriter. file)]
    (binding [*out* w *print-dup* true] (prn frm))))

(defn frm-load
  "Load a clojure form from file."
  [#^java.io.File file]
  (with-open [r (java.io.PushbackReader.
     (java.io.FileReader. file))]
     (let [rec (read r)]
      rec)))

Have a look at clojure.core_print.clj in the clojure source for details on print-dup.

Rgds, Adrian.

Adrian Cuthbertson

unread,
Jun 23, 2009, 11:35:31 PM6/23/09
to clo...@googlegroups.com
Sorry, (prn frm) should have been (prn form).

ogcraft

unread,
Jun 24, 2009, 3:40:50 AM6/24/09
to Clojure
Thanks a lot, Adrian.
Reply all
Reply to author
Forward
0 new messages