Skip to content

Commit a52e369

Browse files
committed
feat: let-traced convenience macro
1 parent 58486bd commit a52e369

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/gen/dynamic.cljc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,28 @@
211211
[& args]
212212
{:clj-kondo/lint-as 'clojure.core/fn}
213213
(apply gen-body args))
214+
215+
(defmacro let-traced
216+
"Similar to `clojure.core/let`, but wraps all values in [[trace!]] calls
217+
addressed via the binding symbol.
218+
219+
Example usage:
220+
221+
```clojure
222+
(def func
223+
(gen []
224+
(let-traced [a (gen.distribution/delta \"face\")
225+
b (gen.distribution/delta \"cake\")]
226+
(str a \",\" b))))
227+
228+
(into {} (gf/simulate func []))
229+
;; => {:a \"face\" :b \"cake\"}
230+
```"
231+
[bindings & body]
232+
(let [bents (partition 2 bindings)]
233+
(assert (every? symbol? (map first bents)))
234+
`(let ~(into []
235+
(mapcat (fn [[sym expr]]
236+
[sym `(trace! (quote ~sym) ~@expr)]))
237+
bents)
238+
~@body)))

0 commit comments

Comments
 (0)