Skip to content

Instantly share code, notes, and snippets.

@tomfaulhaber
Created July 5, 2009 15:55
Show Gist options
  • Save tomfaulhaber/141001 to your computer and use it in GitHub Desktop.
Save tomfaulhaber/141001 to your computer and use it in GitHub Desktop.
(ns wrap-base
(:use [clojure.contrib.pprint :only (cl-format pprint)]))
(def *print-base* 10)
;;; borrowed frm cl-format
(defn- integral?
"returns true if a number is actually an integer (that is, has no fractional part)"
[x]
(cond
(integer? x) true
(decimal? x) (>= (.ulp (.stripTrailingZeros (bigdec 0))) 1) ; true iff no fractional part
(float? x) (= x (Math/floor x))
(ratio? x) (let [#^clojure.lang.Ratio r x]
(= 0 (rem (.numerator r) (.denominator r))))
:else false))
(def orig-pr pr)
(defn pr-with-base
([x]
(if (and (integral? x) (not (zero? x)))
(cl-format true "~vr" *print-base* x)
(orig-pr x)))
([x y & rest]
(pr-with-base x)
(print " ")
(apply pr-with-base y rest)))
(defn my-pprint [obj]
(binding [pr pr-with-base]
(pprint obj)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment