Skip to content

Instantly share code, notes, and snippets.

@puredanger
Last active January 3, 2016 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save puredanger/8468561 to your computer and use it in GitHub Desktop.
Save puredanger/8468561 to your computer and use it in GitHub Desktop.
(defn bar-chart-from-ranges-by-name
[data step-size]
{:pre [(not (nil? data))
(not (empty? data))
(> step-size 0)]}
(let [enc-begin (apply min (map first (vals data)))
enc-end (apply max (map second (vals data)))
enc-length (- enc-end enc-begin)
record-list (->> data
(map (fn [[k [b e]]]
[k
(quot (- b enc-begin) step-size)
(quot (- e enc-begin) step-size)]))
(filter (fn [[k b e]] (>= (- e b) 1)))
(sort-by second))
longest-name (last (sort (map #(count (first %)) record-list)))
bar-length (quot enc-length step-size)]
(clojure.string/join
\newline
(map (fn [[k b e]]
(str (format (str "%" longest-name "s :") k)
(apply str (repeat b "-"))
(apply str (repeat (- e b) "*"))
(apply str (repeat (- bar-length e) "-"))))
record-list))))
(println
(bar-chart-from-ranges-by-name
{"Action1" [0 10]
"Act2" [10 30]
"Action3" [20 50]
"Trivial" [0 5]}
10))
@puredanger
Copy link
Author

I would not actually write this code in one big function but I was trying to make it comparable to the blog examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment