Skip to content

Instantly share code, notes, and snippets.

@xpe xpe/grime-dice.clj
Created Jan 16, 2015

Embed
What would you like to do?
(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
You can’t perform that action at this time.