braveandtrue

https://www.braveclojure.com/
moo 2018-09-04T19:05:40.000100Z

Hi Brave and True folks

moo 2018-09-04T19:07:20.000100Z

I’m still not super clear on destructuring. I’m on the macros chapter doing ex. 3. I have the 1-arity version working. But the single vector version doesn’t work, and I’m confused about how the destructuring syntax should work.

(defmacro create-attrs-fn
  ([fnname attribute]
   `(def ~fnname (comp ~attribute :attributes)))
  ([[fnname attribute & remaining]]
   (do (create-attrs-fn fnname attribute
        (create-attrs-fn remaining)))))

moo 2018-09-04T19:27:08.000100Z

because of not understanding destructuring I can’t figure out what’s the arity of my functions.

moo 2018-09-04T19:27:28.000100Z

For example:

moo 2018-09-04T19:29:11.000100Z

(defn mytest [fnname attribute] "nothing")
(:arglists (meta #'mytest))
;=> ([fnname attribute]) 
The above looks like 2-arity. However, what’s this:
(defn mytest [[fnname attribute & remaining] v] "nothing")
(:arglists (meta #'mytest))
;=> ([[fnname attribute & remaining] v])

moo 2018-09-04T19:31:20.000100Z

I’m using this from SO:

(defn arg-count [f]
  (let [m (first (.getDeclaredMethods (class f)))
        p (.getParameterTypes m)]
    (alength p)))
Let’s see if that helps.

moo 2018-09-04T19:35:25.000100Z

Hmm… I don’t get what’s up from my error messages.

(defn mytest [[fnname attribute & remaining]] "nothing")
(arg-count mytest)
; => 1
So, why do I get an error that I can’t call my macro with 1-arity with this definition?
(defmacro create-attrs-fn
  ([[fnname attribute & remaining]]
   (do (create-attrs-fn fnname attribute)
       (create-attrs-fn ~remaining)))
  ([fnname attribute]
   `(def ~fnname (comp ~attribute :attributes))))

moo 2018-09-04T20:33:55.000100Z

:man-shrugging: