Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created November 15, 2011 21:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcromartie/3517ed2c343bbbc3adae to your computer and use it in GitHub Desktop.
Save jcromartie/3517ed2c343bbbc3adae to your computer and use it in GitHub Desktop.
sparklines in Clojure
(defn normalize
"Returns a sequence of normalized (0 to 1.0) numbers in coll."
[coll]
(let [min (apply min coll)
r (- (apply max coll) min)]
(map #(/ (- % min) r) coll)))
(defn spark
"Returns UTF-8 sparkline string for numbers."
[nums]
(let [blocks "▁▂▃▄▅▆▇"
scale (-> blocks count dec)]
(->> nums
normalize
(map #(nth blocks (-> (* scale %) double Math/round)))
(apply str))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment