Created
May 7, 2014 11:41
-
-
Save ctford/628e668283e01452b32d 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
(ns polymorphism.core) | |
(def basket | |
[{:price 88 :name "Chocolate"} | |
{:price 22 :name "Newspaper"} | |
{:price 3 :name "Lettuce"} ]) | |
(defn receipt [items] | |
(map | |
#(str (:price %) " " (:name %)) | |
items)) | |
(defmulti tax (fn [item] (:kind item))) | |
(defmethod tax :food [item] (-> item :price (* 1.2))) | |
(defmethod tax :alcohol [item] (-> item :price (* 1.4))) | |
(defmethod tax :default [item] (-> item :price (* 1.3))) | |
(defn receipt-multi [items] | |
(map | |
#(str (tax %) " " (:name %)) | |
items)) | |
(defprotocol Taxable | |
(taxed [item])) | |
(defrecord food [name] Taxable (taxed [item] (-> item :price (* 1.2)))) | |
(defn receipt-protocol) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment