Skip to content

Instantly share code, notes, and snippets.

@tbl3rd
Last active April 9, 2018 12:54
Show Gist options
  • Save tbl3rd/0cb5bff73b3bc2db86ed12f12d2d5991 to your computer and use it in GitHub Desktop.
Save tbl3rd/0cb5bff73b3bc2db86ed12f12d2d5991 to your computer and use it in GitHub Desktop.

Clojure now has many tools for debugging.

  • Cider NREPL debugger
  • clojure.tools.trace
  • #dbg
  • :break/when metadata
  • pre- and post-conditions
  • clojure.spec

good old println

  • println for a line of human-readable output
  • prn for a line of clojure-core.read able
  • pprint for multi-line values

C-cC-p is cider-pprint-eval-last-sexp

  • Demonstrate

doto macro

  • support fluid method calls

local def

  • Demonstrate

def-let

http://www.learningclojure.com/2010/09/astonishing-macro-of-narayan-singhal.html

(defmacro def-let
  [bindings & more]
  (let [let-expr (macroexpand `(let ~bindings))
        names-values (partition 2 (second let-expr))
        defs   (map #(cons 'def %) names-values)]
    (concat (list 'do) defs more)))

dump-> fn

Add (debug->> LABEL) to a -> form to print [LABEL CURRENT] where CURRENT is the threaded value at that step.

(require '[clojure.pprint :refer [pprint]])
(defn dump->
  "Pretty-print [LABEL OBJECT] from a -> form when debugging."
  [object label]
  (pprint [label object])
  object)

dump->> fn

Add (debug->> LABEL) to a ->> form to print [LABEL CURRENT] where CURRENT is the threaded value at that step.

(require '[clojure.pprint :refer [pprint]])
(defn dump->>
  "Pretty-print [LABEL OBJECT] from a ->> form when debugging."
  [label object]
  (pprint [label object])
  object)

dump macro

(defmacro dump
  "Dump a vector [LABEL VALUE] where FORM is the EXPRESSION source
  and VALUE is its value."
  [expression]
  `(let [x# ~expression]
     (do
       (println ['~expression x#])
       x#)))

debug-do

(defmacro debug-do [& body]
  (when *debug*
    `(do ~@body)))

:break/when

(dotimes [i 10]
  #dbg ^{:break/when (= i 7)}
  (prn i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment