Created
July 5, 2009 15:55
-
-
Save tomfaulhaber/141001 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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