-
Notifications
You must be signed in to change notification settings - Fork 0
/
19_datatypes.clj
50 lines (39 loc) · 1.42 KB
/
19_datatypes.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
(ns koans.19-datatypes
(:require [koan-engine.core :refer :all]))
(defrecord Nobel [prize])
(deftype Pulitzer [prize])
(defprotocol Award
(present [this recipient]))
(defrecord Oscar [category]
Award
(present [this recipient]
(print (str "Congratulations on your "
(:category this) " Oscar, "
recipient
"!"))))
(deftype Razzie [category]
Award
(present [this recipient]
(print (str "You're really the "
category ", "
recipient
"... sorry."))))
(meditations
"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"))))