forked from marick/fp-oo
/
message-class-exercises.clj
279 lines (226 loc) · 8.75 KB
/
message-class-exercises.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
;; For exercise 2
(send-to Klass :new
'Snooper 'Anything
{
:snoop
(fn [& args]
[
(send-to *active-message* :name)
(send-to *active-message* :holder-name)
(send-to *active-message* :args)
(send-to *active-message* :target)
])
}
{})
(def snooper (send-to Snooper :new))
(prn (send-to snooper :snoop "an arg")) ; => [:snoop 'Snooper ("an arg") snooper])
;; For exercise 3
(comment
;; Here are the two methods you are to make work.
;; They're commented out so that they don't break my
;; automated tests.
(def repeat-to-super
(fn []
(activate-method (send-to *active-message* :move-up))))
(def send-super
(fn [& args]
(activate-method (assoc (send-to *active-message* :move-up)
:args args))))
;; Since you're replacing `using-method-above`, it's useful to
;; define it to something that will blow up if there's still a
;; stray call to it.
(def using-message-above :ensure-that-the-function-can-no-longer-be-called)
)
(send-to Klass :new
'SubSnooper 'Snooper
{
:snoop
(fn [& args]
;; Need to try `move-up`
;; in the context of a method
;; that shadows a method in the
;; superclass.
(send-to *active-message* :move-up))
:fail-dramatically
(fn []
(send-to *active-message* :move-up))
:to-string
(fn []
(str "Look! " (repeat-to-super)))
}
{})
(comment
;; The following is commented out because of the way my
;; automated tests work. It's in a block comment to make
;; it easy for you to copy it into a REPL
(def snooper (send-to SubSnooper :new))
(prn (send-to snooper :snoop "an arg")) ; => {:name :snoop,
; :holder-name Snooper,
; :args ("an arg"),
; :target {:__left_symbol__ SubSnooper},
; :__left_symbol__ Message}
(prn (activate-method (send-to snooper :snoop "an arg"))) ; => [:snoop Snooper ("an arg")
; {:__left_symbol__ SubSnooper}]
(send-to snooper :fail-dramatically) ; throws Error
(prn (send-to snooper :to-string)) ; => "Look! {:__left_symbol__ SubSnooper}"
)
;; ;; For exercise 6
(send-to Klass :new
'Criminal 'Anything
{
:taunt
(fn [copper]
(let [taunt "Ha ha copper! You'll never catch me!"]
(println "Criminal:" taunt)
(send-to copper :be-taunted taunt)))
:give-yourself-up
(fn []
(let [confession "It's a fair cop, but society is to blame."]
(println "Criminal:" confession)))
}
{})
(send-to Klass :new
'Police 'Anything
{
:add-instance-values
(fn [name]
(assoc this :name name))
:name (fn [] (:name this))
:be-taunted
(fn [taunt]
(let [evildoer (send-to *active-message* :sender)]
(cl-format true "Detective ~A: Wot? Who?~%"
(send-to this :name))
(println "<nab/>")
(send-to evildoer :give-yourself-up)))
}
{})
(comment
(def criminal (send-to Criminal :new))
(def police (send-to Police :new "Biggles"))
(send-to criminal :taunt police)
)
;; ;; For Exercise 7
(declare Bottom Middle Top)
(send-to Klass :new
'Bottom 'Middle
{
:add-instance-values
(fn [value]
(assoc this :value value))
:chained-message (fn [n]
(send-to (send-to Bottom :new "two")
:secondary-message (* 10 n)))
:secondary-message (fn [n] (repeat-to-super))
}
{})
(send-to Klass :new
'Middle 'Top
{
:secondary-message (fn [n] (send-super (* 10 n)))
:tertiary-message (fn [n] (send-super (* 10 n)))
}
{})
(send-to Klass :new
'Top 'Anything
{
:secondary-message (fn [n] (send-to this :tertiary-message (* 10 n)))
:tertiary-message (fn [n] *active-message*)
}
{})
(comment
;; It might be helpful to start with simple cases and work up.
(def traceful (send-to (send-to Top :new) :tertiary-message 1))
traceful
(send-to traceful :trace)
(def traceful (send-to (send-to Middle :new) :tertiary-message 1))
traceful
(pprint (send-to traceful :trace))
(def traceful (send-to (send-to Bottom :new "one") :chained-message 1))
traceful
(pprint (send-to traceful :trace))
)
;; `friendly-trace can take the output of one of the above lines and
;; print it in a more pleasing form. It shows a few new Clojure
;; features.
;; Optional arguments in Clojure are done in a somewhat awkward way, by
;; giving a list of parameter-list-and-body pairs:
;;
;; (fn ([arglist1] body1...) ([arglist2] body2...))
;;
;; Typically versions with fewer parameters add defaults and call the
;; version with the longest parameter.
;;
;;
;; This example also uses "destructuring args" in a typical way. Consider this
;; `let` expression:
;;
;; (let [ [head & tail] (list 1 2 3)] ...body...)
;;
;; The embeddded vector `[head & tail]` says that the single argument
;; is expected to be a sequence. Its `first` element is bound to `head`, and the
;; `rest` of the sequence is bound to `tail`.
(def target-to-name-map
"In a trace, we want to print a simple name for the
target of a message, rather than the default representation.
We could override the `:to-string` method, but I'm thinking that
how you want to see an object print is situation-dependent. In
the case of a trace, what *I* want is a little table of
contents that shows each separate instance once and gives it a
name that's thereafter used in the trace. This produces a
map that's needed to do that."
(fn
([targets]
(target-to-name-map targets {} {}))
([[target & remainder] class-instance-counts result]
(cond (nil? target)
result
(contains? result target)
(target-to-name-map remainder class-instance-counts result)
:else
(let [target-class-name (send-to target :class-name)
name-suffix (inc (or (get class-instance-counts target-class-name)
0))
short-name (str "a-" target-class-name "-" name-suffix)]
(target-to-name-map remainder
(assoc class-instance-counts
target-class-name name-suffix)
(assoc result
target short-name)))))))
;; If you use a sequence function with a map, each key-value pair is delivered to
;; as a two-element sequence:
;;
;; user=> (map identity {:a 1 :b 2})
;; ([:a 1] [:b 2])
(def table-of-contents
"The 'table of contents' displays the mapping between message targets
and their names. The table is in alphabetical order of names. That
groups instances of a particular class together, in the order they were
created."
(fn [namings]
(map (fn [[target name]] (str name " =stands=for=> " target))
(sort-by second namings))))
(def friendly-trace
"Convert a raw trace into a formatted one, with a
table of contents at both ends. Delegations to `super`
are indented."
(fn [trace]
(let [namings (target-to-name-map (map :target trace))
renamed-trace (map (fn [elt]
(assoc elt
:target (get namings (:target elt))))
trace)
format-one-element
(fn [elt]
(if (zero? (:super-count elt))
(cl-format nil
"(send-to ~A ~A.~A ~{~A~})"
(:target elt) (:holder-name elt) (name (:name elt)) (:args elt))
(cl-format nil
" | delegate ~A to ~A"
(:args elt) (:holder-name elt))))]
(dorun (map println (table-of-contents namings)))
(println "-------------")
(dorun (map println (map format-one-element renamed-trace)))
(println "-------------")
(dorun (map println (table-of-contents namings))))))