Skip to content

Instantly share code, notes, and snippets.

@simon-brooke
Created January 26, 2022 15:51
Show Gist options
  • Save simon-brooke/b234d2d3e83b22150c83508fb5f745b4 to your computer and use it in GitHub Desktop.
Save simon-brooke/b234d2d3e83b22150c83508fb5f745b4 to your computer and use it in GitHub Desktop.
Quick and dirty bargraph from Clojure frequencies
(defn bargraph
"Quick'n'dirty bargraph from these `freqs`, the output from the function
`clojure.core/frequencies`."
[freqs]
(let [u (/ (apply max (vals freqs)) 20)
p (/ (apply +' (vals freqs)) 100)]
(join "\n"
(map #(format "%7d : %s (%d %%)"
%
(apply str (take (/ (freqs %) u) (repeat "#")))
(int (/ (freqs %) p)))
(keys freqs)))))
;; generates output like:
;; 1 : # (0 %)
;; 2 : # (0 %)
;; 3 : #### (4 %)
;; 4 : ############ (17 %)
;; 5 : #################### (28 %)
;; 6 : ################## (25 %)
;; 7 : ################# (23 %)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment