Created
June 2, 2018 10:08
-
-
Save sherpc/b5a8b6b18e2d6a35a097dbf5e0ad78b5 to your computer and use it in GitHub Desktop.
AOP like in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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