Created
July 10, 2011 18:30
-
-
Save amalloy/1074808 to your computer and use it in GitHub Desktop.
This file contains 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
(def denoms [[:twenty 20] | |
[:ten 10] | |
[:five 5] | |
[:dollar 1] | |
[:quarter 1/4] | |
[:dime 1/10] | |
[:nickel 1/20] | |
[:penny 1/100]]) | |
(defn change-denom [denom amount] | |
(let [[label size] denom | |
[num remaining] ((juxt quot rem) amount size)] | |
(into {:remainder remaining} | |
(when-not (zero? num) | |
{label num})))) | |
(defn make-change [amount] | |
(dissoc (apply merge | |
(reductions (fn [{:keys [remainder]} denom] | |
(change-denom denom remainder)) | |
{:remainder amount} | |
denoms)) | |
:remainder)) | |
;; or | |
(defn make-change [amount] | |
(-> {:remainder amount} | |
(reductions (fn [{:keys [remainder]} denom] | |
(change-denom denom remainder)) | |
denoms) | |
(->> (apply merge)) | |
(dissoc :remainder))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment