-
-
Save jcromartie/3517ed2c343bbbc3adae to your computer and use it in GitHub Desktop.
sparklines in Clojure
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
(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