I’ve made a macro! You will now find pez/taplet
on Clojars.
OK. It s not my first macro. But it is my first public macro, and my first non-trivial one. It’s WIP and I would like some input on the names of the macros and also the API a bit.
What it does? There are two macros: let>
and let>-l
. The first one can be used as a drop-in replacement to let
, only that it will also tap>
the binding box. The second one takes a keyword as the first argument and will insert that first as a label in the tapped vector.
My use case if for debugging. I found myself doing some copy/paste and fancy regex search/replace to see what some let bindings actually evaluated to.
Example usage:
(require '[pez.taplet :refer [let> let>l]])
(let> [x 1
y 2]
[x y])
(let>l :foo
[x 1
y 2]
[x y])
These will tap:
[:x 1
:y 2]
[:foo
:x 1
:y 2]
respectively
Destructurings sort of works, a bit. For vector destructurings it is all just flattened:
(let> [[a [b [c d]]] [:foo [:bar [:baz :gaz]]]]
[a b c d])
Taps: [:a :foo :b :bar :c :baz :d :gaz]
For map destructurings it is a bit rawer. I don’t really know how to pick out the bound symbols, so I just output the map as a string, and then the value is the same map, but with the values replacing the symbols (this is output as a map). Maybe it is easiest to show with my test for it:
(testing "Taps map destructuring bind-to as a string"
(let [tapped (atom nil)
save-tap (fn [v] (reset! tapped v))]
(add-tap save-tap)
(is (= [2 {:x 2}]
(sut/let> [{:keys [x] :as y} {:x 2}]
[x y])))
(is (= ["{:keys [x], :as y}" {:keys [2] :as {:x 2}}]
@tapped))
(remove-tap save-tap)))
I tried pretty hard to output the vector using symbols instead of keywords for the ”bind-to”, but my macro-fu did not suffice. Same with the string representation of the destructuring map. I would want a map instead, but couldn’t figure out how to do it. If you peeps know how to do this, please tell me!
Do you think the names for the macros are any good? Have other suggestions? I have deployed it only as a -SNAPSHOT
release for reasons.
You find the repo here. Issues and PRs welcome, of course.