Last active
August 29, 2015 13:57
-
-
Save mowat27/9401080 to your computer and use it in GitHub Desktop.
Glasgow Clojurians - 6 March 2014
This file contains hidden or 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
| ; We had an interesting chat about using collections | |
| ; as functions. In summary... | |
| ; get returns an item from a collection | |
| (get [:a :b :c] 1) | |
| :b | |
| (get {:a 1, :b 2} :b) | |
| 2 | |
| (get #{:a :b :c} :c) | |
| :c | |
| (get #{:a :b :c} :foo) | |
| nil | |
| ; Calling the collection as a function has the same effect | |
| ([:a :b :c] 1) | |
| :b | |
| ({:a 1, :b 2} :b) | |
| 2 | |
| (#{:a :b :c} :c) | |
| :c | |
| (#{:a :b :c} :foo) | |
| nil | |
| ; which is awfully useful when the collection is passed to | |
| ; a higher order function | |
| (map [:a :b :c] [1 2]) | |
| (:b :c) | |
| (map '(:a :b :c) [1 2]) ; sadly, it doesn't work for lists | |
| (map {:a 1, :b 2} [:b :foo]) | |
| (2 nil) | |
| (filter #{:b :c} [:a :b :c :d]) | |
| (:b :c) |
This file contains hidden or 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
| (ns clj-vending.core) | |
| (def nickel 5) | |
| (def dime 10) | |
| (def quarter 25) | |
| (def dollar 100) | |
| (defn calc-change [item-value amount-tendered] | |
| (let [change (- amount-tendered item-value)] | |
| {:amount change :nickel (/ change nickel)})) | |
| (calc-change 60 100) | |
| (defn find-highest-coin [amount] | |
| (cond | |
| (>= amount dollar) [dollar (- amount dollar)] | |
| (>= amount quarter) [quarter (- amount quarter)] | |
| (>= amount dime) [dime (- amount dime)] | |
| (>= amount nickel) [nickel (- amount nickel)])) | |
| (find-highest-coin 45) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment