diff --git a/README.md b/README.md index b5b35ca..490f913 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,9 @@ Launch repl with project in classpath, i.e: `java -cp clojure.jar:vending-machine/src clojure.main` -The load in repl, i.e: +Then load in repl, i.e: -`(use vending-machine.vm)` +`(use 'vm-clojure.vm)` and diff --git a/src/vm_clojure/vm.clj b/src/vm_clojure/vm.clj index b3b108c..8acd833 100644 --- a/src/vm_clojure/vm.clj +++ b/src/vm_clojure/vm.clj @@ -25,6 +25,21 @@ [purchase-price, coins] (<= purchase-price (reduce + coins))) +(defn rm-one + "Remove at most one of 'x' from the collection." + [x coll] + (let [[head & rest] coll] + (if (= x head) + rest + (cons head (rm-one x rest))))) + +(defn rm-these + "Remove at most one of each of the x's from the collection." + [xs coll] + (if (empty? xs) + coll + (rm-these (rest xs) (rm-one (first xs) coll)))) + (defn purchase "Given a purchase-price (cents), a sequence of deposited coins, and a sequence of reserve coins in the vending machine, return a map @@ -35,7 +50,7 @@ (let [change (change-due purchase-price deposits) reserve-plus-deposited (concat deposits reserve) change-coins (make-change change reserve-plus-deposited) - new-reserve (remove #(some #{%} change-coins) reserve-plus-deposited)] + new-reserve (rm-these change-coins reserve-plus-deposited)] {:success true :change change-coins :reserve new-reserve} ) :else {:success false :reserve reserve}