lambdaisland / glogi

A ClojureScript logging library based on goog.log
Mozilla Public License 2.0
119 stars 13 forks source link

Support passing multiple values to spy #22

Closed alysbrooks closed 2 years ago

alysbrooks commented 2 years ago

I often want to pass multiple values to spy, so I added that capability to the macro.

Having worked on this PR, I think the intended usage is wrapping existing expressions with spy, which avoids the need for passing multiple values to spy. Still, I think using spy with multiple values is sometimes useful even if you predominantly wrap existing forms.

plexus commented 2 years ago

Interesting. I don't really have a habit of using spy but I'm fine with this going in. Note though that spy is modelled after io.pedestal.log/spy, so I would also update glogc to support multiple expressions, which will mean reimplementing based on ~io.pedestal.log/debug~ io.pedestal.log/log-expr.

plexus commented 2 years ago

Took me a few minutes to understand your implementation, here's my attempt

(defmacro spy
  ([form]
   (let [res (gensym)]
     `(let [~res ~form]
        ~(log-expr &form :debug [:spy `'~form
                                 :=> res])
        ~res)))
  ([form & forms]
   ;; using cons to make explicit that it's a prepend
   (let [forms (cons form forms)
         syms (repeatedly (count forms) gensym)
         ;; map/mapcat take multiple collections
         bindings (mapcat list syms forms)
         spy-vec (vec (mapcat (fn [form sym]
                                ;; or `'~form as above, but if you don't write a
                                ;; lot of macros that's a bit harder to parse
                                [(list 'quote form) sym])
                              forms
                              syms))]
     `(let [~@bindings]
        ~(log-expr &form :debug [:spy spy-vec])
        ~(last syms)))))
alysbrooks commented 2 years ago

@plexus That is a lot more readable. I'll replace my implementation with yours.

plexus commented 2 years ago

Looks good to me!