I think the best way would be to simply store functions as lists (e.g.
(fn [] ...)) in the file, use clojure.core/read to read the data
structure and store both the list representation and the eval'd
function object in the program. This might be implemented by having a
memoizing 'call' function, take a list and arguments and call the
eval'd list with the arguments, for example:

;;; File "a" contains: {:a-fun (fn [x] (+ 2 x))}

user=> (def data (with-in-str (slurp "a") (read)))
#'user/data
user=> data
{:a-fun (fn [x] (+ 2 x))}
user=> (def call (memoize (fn [l & args]
                           (apply (eval l) args))))
#'user/call
user=> (call (data :a-fun) 3)
5

;;; Also, rationale for memoization:
user=> (defmacro slow-compilation [] (Thread/sleep 1000))
nil
user=> (time (call '(fn [] (slow-compilation) 1)))
"Elapsed time: 1012.398507 msecs"
1
user=> (time (call '(fn [] (slow-compilation) 1)))
"Elapsed time: 0.368762 msecs"
1

;;;;;;


If an additional layer of visible indirection is not adequate, I guess
you can hide it by defining a java class which is constructed with a
list representing the function, evals the list in the constructor,
implements IFn and passes calls to it's methods to the function and
prints itself readably, for example #=(TheClass. '(fn [])). Instances
of the class would otherwise behave like Clojure functions.

--~--~---------~--~----~------------~-------~--~----~
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
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