Skip to content

Instantly share code, notes, and snippets.

@luxbock
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luxbock/38d245ff1f1a194921cb to your computer and use it in GitHub Desktop.
Save luxbock/38d245ff1f1a194921cb to your computer and use it in GitHub Desktop.
boofoo
(defmacro pr-> [& body]
(let [[init & exprs] body
syms (repeatedly (count body) gensym)
vals (mapcat (fn [exp sym prev]
[sym `(when-not (instance? Exception ~prev)
(try ~(if (seq? exp)
`(~(first exp) ~@(next exp) ~prev)
`(~exp ~prev))
(catch Exception e# e#)))])
exprs (rest syms) syms)
ress `(take-while identity ~(vec (rest syms)))]
`(let ~(vec (concat [(first syms) init] vals))
(apply print-table
(format "-> %s" ~init)
(map vector
~(list `quote exprs)
~ress))
(if (instance? Exception (last ~ress))
(throw (last ~ress))
(last ~ress)))))
boofoo.core> (pr-> 2 dec dec (/ 1) inc (+ 5))
┌───────┬───────────────────────────────────────────────┐
│ -> 2 │ value │
├───────┼───────────────────────────────────────────────┤
│ dec ╎ 1 │
│ dec ╎ 0 │
│ (/ 1) ╎ java.lang.ArithmeticException: Divide by zero │
└───────┴───────────────────────────────────────────────┘
ArithmeticException Divide by zero clojure.lang.Numbers.divide (Numbers.java:158)
boofoo.core>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment