Created
November 14, 2022 11:44
-
-
Save mbezjak/0f11c0b550a0751c66903947328f947c to your computer and use it in GitHub Desktop.
Clojure Debugging Helpers
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
(ns defn) | |
(defn symbols [args] | |
(mapcat | |
(fn [a] | |
(cond | |
(map? a) (:keys a) | |
(vector? a) (symbols a) | |
:else [a])) | |
args)) | |
(defmacro d [fn-name & fdecl] | |
(let [[args body] (if (string? (first fdecl)) | |
[(second fdecl) (not-empty (drop 2 fdecl))] | |
[(first fdecl) (not-empty (rest fdecl))]) | |
defs (map (fn [s] `(def ~s ~s)) (symbols args))] | |
`(defn ~fn-name ~args | |
~@defs | |
~@body))) | |
(comment | |
(defn foo []) | |
(defn foo [a b] (+ a b)) | |
(defn foo [{:keys [a b]}] (+ a b)) | |
(d foo "doc" []) | |
(d foo []) | |
(d foo [a b] (+ a b)) | |
(d foo [{:keys [a b]}] (+ a b)) | |
(d foo [& nums] (apply + nums)) | |
(d foo [[a b]] (+ a b)) | |
(d foo [[{:keys [a b]}]] (+ a b)) | |
(d foo [& {:keys [a b]}] (+ a b)) | |
(foo) | |
(foo 1 2) | |
(foo {:a 1 :b 2}) | |
(foo [1 2]) | |
(foo [{:a 1 :b 2}]) | |
(foo :a 1 :b 2)) |
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
(ns let) | |
(defmacro d [bindings & body] | |
(let [bindings-with-defs (->> bindings | |
(destructure) | |
(partition 2) | |
(mapcat (fn [[s code]] | |
[s code | |
(gensym "not-used") `(def ~s ~s)])))] | |
`(let [~@bindings-with-defs] | |
~@body))) | |
(comment | |
(let [a (+ 1 2) | |
b (inc a) | |
{:keys [c d]} {:c 1 :d 5}] | |
(+ a b c d)) | |
(destructure '[a (+ 1 2) | |
b (inc a) | |
{:keys [c d]} {:c 1 :d 5}]) | |
(d [a (+ 1 2) | |
b (inc a) | |
{:keys [c e]} {:c 1 :e 5}] | |
(+ a b c e)) | |
(d [a (+ 1 2) | |
b (throw (ex-info "Can you see a?" {}))] | |
:not-relevant)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment