Skip to content

Instantly share code, notes, and snippets.

@xpe
Created January 16, 2015 17:56
Show Gist options
  • Save xpe/30ae93b107c91ec2ccf5 to your computer and use it in GitHub Desktop.
Save xpe/30ae93b107c91ec2ccf5 to your computer and use it in GitHub Desktop.
(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