document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. ;; Version 1: direct inline the anonymous function:
  2. (defn as-currency
  3.   "Money amounts are transmitted as \\"$2.44\\".
  4.  Parse this and return a numeric type."
  5.   [currency-amount]
  6.   (-> (subs currency-amount 1)
  7.       ((fn [x] (println x) x))
  8.       (bigdec)
  9.       ((fn [x] (println x) x))))
  10.  
  11. ;; Version 2: use the inspect function described earlier:
  12. (defn inspect
  13.   "Print out a value for debugging. Optional arguments are appended for
  14.  information."
  15.   ([val]
  16.    (println val)
  17.    val)
  18.   ([val & msgs]
  19.    (println val (str/join " " (cons "--" msgs)))
  20.    val))
  21.  
  22. (defn as-currency
  23.   "Money amounts are transmitted as \\"$2.44\\".
  24.  Parse this and return a numeric type."
  25.   [currency-amount]
  26.   (-> (subs currency-amount 1)
  27.       (inspect "stripped")
  28.       (bigdec)
  29.       (inspect "as bigdec")))
  30.  
  31. ;;;; Evaluating the sample code prints the following:
  32.  
  33. ;; 1200.20 -- stripped
  34. ;; 1200.20M -- as bigdec
  35. ;; 0.03 -- stripped
  36. ;; 0.03M -- as bigdec
  37. ;; 120.00 -- stripped
  38. ;; 120.00M -- as bigdec
  39. ;; 5.99 -- stripped
  40. ;; 5.99M -- as bigdec
  41. ;; 20 -- stripped
  42. ;; 20M -- as bigdec
  43. ;; 12.33 -- stripped
  44. ;; 12.33M -- as bigdec
  45. ;; $4.50 -- stripped
  46. ;; NumberFormatException   java.math.BigDecimal.<init> (BigDecimal.java:494)
');