Skip to content

Instantly share code, notes, and snippets.

@sherpc
Created June 2, 2018 10:08
Show Gist options
  • Save sherpc/b5a8b6b18e2d6a35a097dbf5e0ad78b5 to your computer and use it in GitHub Desktop.
Save sherpc/b5a8b6b18e2d6a35a097dbf5e0ad78b5 to your computer and use it in GitHub Desktop.
AOP like in Clojure
(defn- add-call-log
[s]
(when-let [v (resolve s)]
(when-not (-> v meta :macro)
(let [current @v
patched (fn [& args]
(prn {:called-fn v :args args})
(.applyTo ^clojure.lang.IFn current args))]
(alter-var-root v (constantly patched))))))
user.my> (defn minus [x y] (- x y))
#'user.my/minus
user.my> (minus 3 1)
2
user.my> (add-call-log 'minus)
#function[user.my/add-call-log/patched--48026]
user.my> (minus 3 1)
{:called-fn #'user.my/minus, :args (3 1)}
2
user.my>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment