Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
48 lines (37 sloc) 1.36 KB
(defrecord Nobel [prize])
(deftype Pulitzer [prize])
(defprotocol Award
(present [this recipient]))
(defrecord Oscar [category]
(present [this recipient]
(print (str "Congratulations on your "
(:category this) " Oscar, "
(deftype Razzie [category]
(present [this recipient]
(print (str "You're really the "
(.category this) ", "
"... sorry."))))
"Holding records is meaningful only when the record is worthy of you"
(= "peace" (.prize (Nobel. "peace")))
"Types are quite similar"
(= "literature" (.prize (Pulitzer. "literature")))
"Records may be treated like maps"
(= "physics" (:prize (Nobel. "physics")))
"While types may not"
(= nil (:prize (Pulitzer. "poetry")))
"Further study reveals why"
(= [true false]
(map map? [(Nobel. "chemistry")
(Pulitzer. "music")]))
"Either sort of datatype can define methods in a protocol"
(= "Congratulations on your Best Picture Oscar, Evil Alien Conquerors!"
(with-out-str (present (Oscar. "Best Picture") "Evil Alien Conquerors")))
"Surely we can implement our own by now"
(= "You're really the Worst Picture, Final Destination 5... sorry."
(with-out-str (present (Razzie. "Worst Picture") "Final Destination 5"))))