From a52e3692190e99804ba1ef3b65be0e22bce84d56 Mon Sep 17 00:00:00 2001 From: Sam Ritchie Date: Mon, 2 Oct 2023 11:49:48 -0600 Subject: [PATCH] feat: let-traced convenience macro --- src/gen/dynamic.cljc | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/gen/dynamic.cljc b/src/gen/dynamic.cljc index 05080ca..e911702 100644 --- a/src/gen/dynamic.cljc +++ b/src/gen/dynamic.cljc @@ -211,3 +211,28 @@ [& args] {:clj-kondo/lint-as 'clojure.core/fn} (apply gen-body args)) + +(defmacro let-traced + "Similar to `clojure.core/let`, but wraps all values in [[trace!]] calls + addressed via the binding symbol. + + Example usage: + + ```clojure + (def func + (gen [] + (let-traced [a (gen.distribution/delta \"face\") + b (gen.distribution/delta \"cake\")] + (str a \",\" b)))) + + (into {} (gf/simulate func [])) + ;; => {:a \"face\" :b \"cake\"} + ```" + [bindings & body] + (let [bents (partition 2 bindings)] + (assert (every? symbol? (map first bents))) + `(let ~(into [] + (mapcat (fn [[sym expr]] + [sym `(trace! (quote ~sym) ~@expr)])) + bents) + ~@body)))