Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Created September 26, 2014 09:24
Show Gist options
  • Save pyrtsa/6848a49222270232c414 to your computer and use it in GitHub Desktop.
Save pyrtsa/6848a49222270232c414 to your computer and use it in GitHub Desktop.
Unicode bar charts in Clojure
(defn box-vert [q]
(let [n (* q 8)]
(if (< n 1)
\space
(char (+ 0x2580 (min n 8))))))
(defn box-horz [q]
(let [n (* q 8)]
(if (< n 1)
\space
(char (- 0x2590 (min n 8))))))
(defn bar
[f x]
(->> (iterate dec x)
(take-while pos?)
(map f)
(apply str)))
(def bar-horz (partial bar box-horz))
(def bar-vert (partial bar box-vert))
(defn hbars
[xs]
(doseq [x xs]
(println (bar-horz x))))
(defn vbars
[xs]
(let [bars (map (comp bar-vert) xs)
n (apply max (map count bars))]
(doseq [i (reverse (range n))]
(apply println (map #(get % i \space) bars)))))
(comment
(def xs [9.6 7.3 10.5 10 9])
(hbars xs)
;; █████████▋
;; ███████▍
;; ██████████▌
;; ██████████
;; █████████
(vbars xs)
;; ▄
;; ▄ █ █
;; █ █ █ █
;; █ ▂ █ █ █
;; █ █ █ █ █
;; █ █ █ █ █
;; █ █ █ █ █
;; █ █ █ █ █
;; █ █ █ █ █
;; █ █ █ █ █
;; █ █ █ █ █
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment