> 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)?
If you are only interested in the function (the var won't be rebound), you can try just keeping a set of functions. If there is the chance the var will be rebound, and you are specifically interested in the current value of the var, then you can keep a set of vars. You can use a macro to make the add-to-hook take the function symbol so it looks more like adding the function instead of having to do all of the #'fn syntax. See below for examples. Sincerely, Daniel Solano Gómez (ns vars) (def fn-hook (atom #{})) (defn add-to-fn-hook [fn] (swap! fn-hook conj fn)) (defn run-fn-hook [] (doseq [fn @fn-hook] (fn))) (def var-hook (atom #{})) (defn add-to-var-hook [fn-var] (swap! var-hook conj fn-var)) (defn run-var-hook [] (doseq [var-fn @var-hook] ((deref var-fn)))) (defn test-fn [] (println "Hi!")) (add-to-fn-hook test-fn) (add-to-fn-hook test-fn) (add-to-var-hook #'test-fn) (add-to-var-hook #'test-fn) ; both print "Hi!" (run-fn-hook) (run-var-hook) (defn test-fn [] (println "Bye!")) (add-to-fn-hook test-fn) (add-to-fn-hook test-fn) (add-to-var-hook #'test-fn) (add-to-var-hook #'test-fn) (run-fn-hook) ; prints "Hi!" and "Bye!" (run-var-hook) ; only prints "Bye!" (reset! var-hook #{}) (defmacro add-to-hook-macro [fn] `(add-to-var-hook (var ~fn))) (add-to-hook-macro test-fn) (add-to-hook-macro test-fn) (run-var-hook) ; prints "Bye!" (defn test-fn [] (println "Hi!")) (add-to-hook-macro test-fn) (add-to-hook-macro test-fn) (run-var-hook) ; prints "Hi!"
pgpGAdTAPdEre.pgp
Description: PGP signature