Fork me on GitHub
#braveandtrue
<
2018-09-04
>
moo19:09:40

Hi Brave and True folks

moo19:09:20

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)))))

moo19:09:08

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

moo19:09:28

For example:

moo19:09:11

(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])

moo19:09:20

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.

moo19:09:25

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))))

moo20:09:55

:man-shrugging: