Clojure print values ​​in `let` binding

What is the idiomatic way to print values ​​inside a binding let?

When I started developing in Clojure, I wrote code in REPL, and then I turned into simple expressions let. As a newbie, I often made mistakes during this (simple) phase of transformation.

(let [a (aFn ...)
       b (bFn ... a)]
   ;; error above
)

So, I would turn it into something similar, basically an attachment:

(println "a is"    (aFn ...))
(println "b is" (bFn ... (aFn ...)))
(let [a (aFn ...)
       b (bFn ... a)]
   ;; ...
)

It works most of the time thanks to Clojure good (immutability, referential transparency ..).

Now I am doing something line by line:

(let [a (aFn ...)
       _ (println "a is" a)
       b (bFn ... a)
      _ (println "b is" b)]
   ;; ...
)

This is an improvement, but it still seems awkward. What is the right way to do this?

+4
source share
2 answers

You can define a print function that returns its argument:

(defn cl-print [x] (doto x (print)))

:

(let [a (cl-print (aFn ...))
      b (cl-print (bFn ... a))]
   ...)
+2

. let. , , . , , , . , ..

, debug. , , let. var-debug-level, . var , /.

"" , , , , , , , .

, , - , " " .

, / . , , . , , , . , , .

- ,

(def debug-level 20)

(defn debug [lvl prefix val]
  (if (>= lvl debug-level)
    (println (str prefix ": " val)))

(defn debug1 [prefix v]
  (debug 10 prefix v))

(defn debug2 [prefix v]
  (debug 20 prefix v))

etc.

and then just call

(debug2 :a a)

in the body of your function to have a fingerprint value when the debug level is 20 or higher.

+3
source

All Articles