Skip to content

Commit

Permalink
bug fix, fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbale committed Aug 24, 2011
1 parent 14280cb commit 6d649e5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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

Expand Down
17 changes: 16 additions & 1 deletion src/vm_clojure/vm.clj
Expand Up @@ -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
Expand All @@ -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}
Expand Down

0 comments on commit 6d649e5

Please sign in to comment.