G'day all,

Is it considered unusual to pass a var into a function? My use-case is
as follows: I want code to be able to register a function on a hook
(kinda like emacs). Each of the registered functions should be called
once on a particular well-defined event. I want the process of
registering a particular function to be idempotent (i.e. if someone
adds the same function twice it is only registered and called once).
The first way that I've thought of doing this is as follows:


(def hook (atom {}))

(defn add-to-hook [function-var]
  (swap! hook
         (fn [x] (assoc x function-var @function-var))))

(defn run-hook []
  (doseq [f (keys @hook)]
    (f)))

Which is used like this:

(defn test [] nil)

(add-to-hook #'test)

Is that considered bad practice and if so can you think of a nicer way
of doing it? It seems like a very weird API... Is there a way I can
pass in the function and determine its var internally? Is there some
alternative/better way of determining function uniqueness or will I
always be defeated somehow (e.g. by importing a function into a new
namespace)?


Thanks,
David

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