Phil Cooper / Aug 29 2024
def-let
{:deps {org.clojure/clojure {:mvn/version "1.10.3"}
compliment/compliment {:mvn/version "0.3.9"}}}
Extensible Data Notation
(defmacro def-let [bindings & body]
(assert (= 0 (mod (count bindings) 2)) "Must have an even number of bindings")
(do (->> bindings
(partition 2)
(map (fn [[s v]] (list def s v))))
body))
0.2s
(str (macroexpand (def-let [a 4
b (* a 100)
a (+ a b)]
a)))
0.0s
(defmacro def-let
"this is the standard let macro with the exception that
it promotes all of the let binding variables to global scope
Best for interactive investigation of a ket structure"
[bindings & body]
(assert (vector? bindings) "expected a vector for bindings")
(assert (even? (count bindings))) "must have an even number of forms in binding vector"
(let [destructured (destructure bindings)
global-defs (->> destructured
(partition 2)
(remove (comp (partial re-seq "(vec|map)__")
name
first))
(map (concat (list def) %)))]
(let* destructured
(do
global-defs)
body)))
0.1s
(def-let [a 100
{:keys [aa] :as m} {:aa 33}
[x y] [11 12]]
(+ x a))
;; later outside the let binding:
(+ a aa x)
0.0s