Last active
January 3, 2016 13:19
-
-
Save puredanger/8468561 to your computer and use it in GitHub Desktop.
The Clojure version of code from http://en.lucida.me/blog/a-taste-of-functional-programming-in-java-7/
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 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would not actually write this code in one big function but I was trying to make it comparable to the blog examples.