clojure - let forms : How to access destructured symbols in a macro? -
i'm trying write macro expands let form destructuring. problem have list of symbols defined in let form, including obtained destruturing.
use case
i'm trying factor out kind of behavior, validation example :
(let [a (foo bar) {x :x, y :y, {u :u, v: v :as nested-map} :nested} some-map] (and x y nested-map u v ; testing truthiness (valid-a? a) (valid-x? x) (valid-y? y) (valid-nested? nested-map) (valid-u-and-v? u v) ))
proposed solution
it nice achieve through sort of and-let
macro call this:
(and-let [a (foo bar) {x :x, y :y, {u :u, v: v :as nested-map} :nested} some-map] (valid-a? a) (valid-x? x) (valid-nested? nested-map) (valid-u-and-v? u v))
what i'm missing
but i'm missing way of accessing list of symbols bound in let form. if had list-bound-symbols
function, :
(defmacro and-let "expands , close previouly checks values declared in bindings truthy, followed tests." [bindings & tests] (let [bound-symbols (list-bound-symbols bindings) ;; i'm missing ] `(let ~bindings (and ~@bound-symbols ~@tests) )))
has got clue how might this?
destructuring handled clojure.core/destructure
function. it's public, can call ourselves , extract names of locals, including naming intermediate results used in destructuring:
(defmacro and-let [bindings & tests] (let [destructured (destructure bindings)] `(let ~destructured (and ~@(take-nth 2 destructured) ~@tests))))
seems work:
(let [foo nil] (and-let [a 1 [b c] [2 3]] (nil? foo))) ;= true
Comments
Post a Comment