Skip to content

Instantly share code, notes, and snippets.

@c-garcia
Created April 2, 2015 07:33
Show Gist options
  • Save c-garcia/83e42f274ad91b059f66 to your computer and use it in GitHub Desktop.
Save c-garcia/83e42f274ad91b059f66 to your computer and use it in GitHub Desktop.
make an text histogram from a vector of integers
;; From the REPL
(require '[clojure.math.numeric.tower :as math])
(defn histo
[v & {:keys [size] :or {size 40}}]
(letfn [(digits-for-num [x]
(inc
(int (math/floor (/ (Math/log x) (Math/log 10))))))]
(let [mm (apply max v)
digits-x (digits-for-num (count v))
digits-y (digits-for-num mm)
frmt-spec-x (str "%0" digits-x "d")
frmt-spec-y (str "%0" digits-y "d")
f (/ (float size) mm)]
(map-indexed
(fn [i e]
(let [ne (Math/round (* f e))
x-l (format frmt-spec-x i)
y-l (format frmt-spec-y e)
stars (apply str (repeat ne "*"))]
(str x-l ":" stars "(" y-l ")")))
v))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment