Skip to content

Instantly share code, notes, and snippets.

@Sophia-Gold
Created February 12, 2018 00:06
Show Gist options
  • Select an option

  • Save Sophia-Gold/32429e51bd8c3afcdc686edb3d0862a7 to your computer and use it in GitHub Desktop.

Select an option

Save Sophia-Gold/32429e51bd8c3afcdc686edb3d0862a7 to your computer and use it in GitHub Desktop.
debugging clojure thread macros
;; Version 1: direct inline the anonymous function:
(defn as-currency
"Money amounts are transmitted as \"$2.44\".
Parse this and return a numeric type."
[currency-amount]
(-> (subs currency-amount 1)
((fn [x] (println x) x))
(bigdec)
((fn [x] (println x) x))))
;; Version 2: use the inspect function described earlier:
(defn inspect
"Print out a value for debugging. Optional arguments are appended for
information."
([val]
(println val)
val)
([val & msgs]
(println val (str/join " " (cons "--" msgs)))
val))
(defn as-currency
"Money amounts are transmitted as \"$2.44\".
Parse this and return a numeric type."
[currency-amount]
(-> (subs currency-amount 1)
(inspect "stripped")
(bigdec)
(inspect "as bigdec")))
;;;; Evaluating the sample code prints the following:
;; 1200.20 -- stripped
;; 1200.20M -- as bigdec
;; 0.03 -- stripped
;; 0.03M -- as bigdec
;; 120.00 -- stripped
;; 120.00M -- as bigdec
;; 5.99 -- stripped
;; 5.99M -- as bigdec
;; 20 -- stripped
;; 20M -- as bigdec
;; 12.33 -- stripped
;; 12.33M -- as bigdec
;; $4.50 -- stripped
;; NumberFormatException java.math.BigDecimal.<init> (BigDecimal.java:494)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment