Skip to content

Instantly share code, notes, and snippets.

@ctford
Created May 7, 2014 11:41
Show Gist options
  • Save ctford/628e668283e01452b32d to your computer and use it in GitHub Desktop.
Save ctford/628e668283e01452b32d to your computer and use it in GitHub Desktop.
(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