Notice that saving the code wouldn't work if you had locals (i.e.
clojures). On the other hand, clojures are serialized when you
serialize the function.

For example, heres some code which saves the function to a file, and
then loads it back
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def f "/home/seth/Desktop/test")
(defn get-bytes
   "convert object to byte array"
   [obj]
   (let [bytes-out (java.io.ByteArrayOutputStream.)
         out-obj (java.io.ObjectOutputStream. bytes-out)]
     (try
       (do (.writeObject out-obj obj) (.toByteArray bytes-out))
       (finally (.close out-obj)))))

(defn my-identity "Copies obj through serialization and
deserialization."
  [obj]
  (let [byte-out (java.io.FileOutputStream. f)
        obj-out  (java.io.ObjectOutputStream. byte-out)]
    (try (.writeObject obj-out obj)
         (finally (.close obj-out)))
    (let [obj-in  (new java.io.ObjectInputStream
                       (java.io.FileInputStream. f))]
      (try (.readObject obj-in)
           (finally (.close obj-in))))))
;;from debug-repl
(defmacro locals
  "Produces a map of the names of local bindings to their values."
  []
  (let [symbols (keys &env)]
    (zipmap (map (fn [sym] `(quote ~sym)) symbols) symbols)))


(meta (my-identity (let [a 2] (with-meta (fn [] 2) {:locals
(locals)}))))
;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;


Also, it is possible to use a custom class loader - I believe
Leiningen and Cake both do this.
Leiningen, i think uses an ant class loader - see
https://github.com/technomancy/leiningen/blob/master/src/leiningen/compile.clj

For defining a classloader in clojure, take a look here
https://github.com/ninjudd/classlojure

So you might want to add to leiningen the ability to specify a
function which returns a classloader and then runs the whole project
in that classloader.  Before doing that, you can experiment with
changing the current threads classloader.

Finally, theres no reason why you couldnt change the clojure core
source to make functions serializable - it wouldnt break anything with
any other code. So, if you could figure out how to make leiningen
import  only your modified clojure core jar (instead of the regular
clojure-1.2.0.jar), it would work nicely.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to