Created
January 16, 2015 17:56
-
-
Save xpe/30ae93b107c91ec2ccf5 to your computer and use it in GitHub Desktop.
Code from my example on https://news.ycombinator.com/item?id=8899387
This file contains hidden or 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
(ns grime-dice | |
(:require | |
[clojure.pprint :refer [print-table]])) | |
(def dice | |
{:red [4 4 4 4 4 9] | |
:yellow [3 3 3 3 8 8] | |
:blue [2 2 2 7 7 7] | |
:magenta [1 1 6 6 6 6] | |
:olive [0 5 5 5 5 5]}) | |
(defn round | |
[x] | |
(Double/parseDouble (format "%.3f" x))) | |
(defn average | |
[xs] | |
(-> (reduce + xs) | |
(/ (count xs)))) | |
(defn exp-sequential | |
[arg] | |
{:pre [(sequential? arg)]} | |
(round (double (average arg)))) | |
(declare exp) | |
(defn exp-map | |
[arg] | |
{:pre [(map? arg)]} | |
(->> arg | |
(map (fn [[k vs]] [k (exp vs)])) | |
(into {}))) | |
(defn exp | |
[arg] | |
(cond (sequential? arg) (exp-sequential arg) | |
(map? arg) (exp-map arg))) | |
(defn order-keys-by | |
"Returns map's keys, ordered by applying f to map values." | |
[f xs] | |
(map first (sort-by second (f xs)))) | |
(defn table | |
[dice] | |
(print-table (order-keys-by exp dice) [(exp dice)])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment