Skip to content

Instantly share code, notes, and snippets.

@danielfm
Created June 15, 2010 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielfm/439264 to your computer and use it in GitHub Desktop.
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.
(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