Created
January 26, 2022 15:51
-
-
Save simon-brooke/b234d2d3e83b22150c83508fb5f745b4 to your computer and use it in GitHub Desktop.
Quick and dirty bargraph from Clojure frequencies
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 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