Skip to content

Instantly share code, notes, and snippets.

@ageldama
Created January 12, 2013 06:57
Show Gist options
  • Select an option

  • Save ageldama/4516496 to your computer and use it in GitHub Desktop.

Select an option

Save ageldama/4516496 to your computer and use it in GitHub Desktop.
(defprotocol Fruit
(subtotal [item]))
(extend-type Orange
Fruit
(subtotal [item] (* 98 (:qty item))))
;;; 'coupon' is the function returing a 'reify' of subtotal. This is
;;; when someone uses a coupon ticket, the price of some fruits is
;;; taken off 25%.
(defn coupon [item]
(reify Fruit (subtotal [_] (int (* 0.75 (subtotal item))))))
(subtotal (Orange. 10))
(subtotal (coupon (Orange. 10)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment