Created
June 15, 2010 15:35
-
-
Save danielfm/439264 to your computer and use it in GitHub Desktop.
Little Clojure program that returns the winners (and each winner's earnings) of a pool.
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
(defstruct bet :name :amount :guess :rate) | |
(defn pool-amount | |
"Returns the sum of all bets' amounts." | |
[poolseq] | |
(reduce + (map :amount poolseq))) | |
(defn pool-rates | |
"Returns the list of bets with their corresponding earning rates." | |
[poolseq] | |
(let [sum (pool-amount poolseq)] | |
(map #(assoc % :rate (/ (:amount %) sum)) poolseq))) | |
(defn pool-winners | |
"Returns the winners with their corresponding proportional earnings." | |
[poolseq result] | |
(let [sum (pool-amount poolseq) | |
winners (filter #(= (:guess %) result) poolseq)] | |
(map #(assoc % :amount (double (* (:rate %) sum))) (pool-rates winners)))) | |
(def my-pool [(struct bet "Daniel" 2.0 [2 0]) ;; Daniel bets $2 on 2x0 | |
(struct bet "John" 4.0 [0 0]) ;; John bets $4 on 0x0 | |
(struct bet "Adriana" 1.0 [2 0])]) ;; Adriana bets $1 on 2x0 | |
(println | |
(pool-winners my-pool [2 0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment