Skip to content

Instantly share code, notes, and snippets.

@amalloy
Created July 10, 2011 18:30
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 amalloy/1074808 to your computer and use it in GitHub Desktop.
Save amalloy/1074808 to your computer and use it in GitHub Desktop.
(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