Permalink
Browse files

Better names and such in code.

  • Loading branch information...
1 parent 793b01e commit f887395fde0248d8fd0e8f6b6deb1c7e0e98c2c8 @marick committed Oct 16, 2012
View
@@ -4,7 +4,7 @@
;;; Exercise 1
(send-to Klass :new
- 'Message 'Anything
+ 'ActiveMessage 'Anything
{
:add-instance-values
(fn [& key-value-pairs]
@@ -21,42 +21,35 @@
;;; Exercise 2
-(def tentative-message
+(def send-to-Message-new ;; Supposed to remind you of (send-to Message :new ...)
+ (fn [target name args holder-name]
+ (let [initializer (get (held-methods 'ActiveMessage) :add-instance-values)]
+ (binding [this (basic-object 'ActiveMessage)]
+ (initializer :name name
+ :holder-name holder-name
+ :args args
+ :target target)))))
+
+(def fresh-active-message
(fn [target name args]
- (let [initialize (get (held-methods 'Message) :add-instance-values)]
- (binding [this (basic-object 'Message)]
- (initialize :name name
- :holder-name (find-containing-holder-symbol
- (:__left_symbol__ target)
- name)
- :args args
- :target target)))))
-
-(def message-or-method-missing-message
- (fn [tentative]
- (if (:holder-name tentative)
- tentative
- (fresh-message (:target tentative)
- :method-missing
- (vector (:name tentative)
- (:args tentative))))))
-
-(def fresh-message
- "Construct the message corresponding to the
+ "Construct the message corresponding to the
attempt to send the particular `name` to the
`target` with the given `args`. If there is no
matching method, the message becomes one that
sends `:method-missing` to the target."
- (fn [target name args]
- (message-or-method-missing-message (tentative-message target name args))))
-
-
+ (let [holder-name (find-containing-holder-symbol (:__left_symbol__ target)
+ name)]
+ (if holder-name
+ (send-to-Message-new target name args holder-name)
+ (fresh-active-message target
+ :method-missing
+ (vector name args))))))
;;; Exercise 3
-(def message-above :ensure-that-the-function-can-no-longer-be-called)
+(def using-message-above :ensure-that-the-function-can-no-longer-be-called)
(send-to Klass :new
- 'Message 'Anything
+ 'ActiveMessage 'Anything
{
:add-instance-values
(fn [& key-value-pairs]
@@ -67,11 +60,11 @@
:args (fn [] (:args this))
:target (fn [] (:target this))
- :move-up ;; <<======
+ :move-up ;; <<======
(fn []
- (let [holder-name (find-containing-holder-symbol
- (method-holder-symbol-above (:holder-name this))
- (:name this))]
+ (let [symbol-above (method-holder-symbol-above (:holder-name this))
+ holder-name (find-containing-holder-symbol symbol-above
+ (:name this))]
(if holder-name
(assoc this :holder-name holder-name)
(throw (Error. (str "No superclass method `" (:name this)
@@ -82,19 +75,18 @@
(def repeat-to-super
(fn []
- (activate (send-to message :move-up))))
+ (activate-method (send-to *active-message* :move-up))))
(def send-super
(fn [& args]
- (def mss message)
- (activate (assoc (send-to message :move-up)
- :args args))))
+ (activate-method (assoc (send-to *active-message* :move-up)
+ :args args))))
;;; Exercise 4
(send-to Klass :new
- 'Message 'Anything
+ 'ActiveMessage 'Anything
{
:add-instance-values
(fn [& key-value-pairs]
@@ -105,27 +97,24 @@
:args (fn [] (:args this))
:target (fn [] (:target this))
- :move-up ;; <<======
+ :move-up
(fn []
- (let [holder-name (send-to this :find-containing-holder-symbol)]
- (if holder-name
+ (let [holder-name (send-to this :holder-name-above)]
+ (if holder-name
(assoc this :holder-name holder-name)
- (send-to this :no-message-to-move-up-to))))
-
- :find-containing-holder-symbol
- (fn []
- (find-containing-holder-symbol (send-to this :next-holder-up)
- (send-to this :name)))
-
- :next-holder-up
- (fn []
- (method-holder-symbol-above (send-to this :holder-name)))
-
- :no-message-to-move-up-to
- (fn []
- (throw (Error. (str "No superclass method `" (send-to this :name)
- "` above `" (send-to this :holder-name)
- "`."))))
+ (send-to this :spew-fail-to-move-up-error))))
+
+ ;; Private
+ :holder-name-above
+ (fn []
+ (let [symbol-above (method-holder-symbol-above (send-to this :holder-name))]
+ (find-containing-holder-symbol symbol-above (send-to this :name))))
+
+ :spew-fail-to-move-up-error
+ (fn []
+ (throw (Error. (str "No superclass method `" (send-to this :name)
+ "` above `" (send-to this :holder-name)
+ "`."))))
}
{})
@@ -137,7 +126,7 @@
;;; Exercise 6
(send-to Klass :new
- 'Message 'Anything
+ 'ActiveMessage 'Anything
{
:add-instance-values
(fn [& key-value-pairs]
@@ -148,55 +137,49 @@
:args (fn [] (:args this))
:target (fn [] (:target this))
:sender (fn [] (:sender this)) ;; <<==
-
+
:move-up
(fn []
- (let [holder-name (send-to this :find-containing-holder-symbol)]
- (if holder-name
+ (let [holder-name (send-to this :holder-name-above)]
+ (if holder-name
(assoc this :holder-name holder-name)
- (send-to this :no-message-to-move-up-to))))
-
- :find-containing-holder-symbol
- (fn []
- (find-containing-holder-symbol (send-to this :next-holder-up)
- (send-to this :name)))
-
- :next-holder-up
- (fn []
- (method-holder-symbol-above (send-to this :holder-name)))
-
- :no-message-to-move-up-to
- (fn []
- (throw (Error. (str "No superclass method `" (send-to this :name)
- "` above `" (send-to this :holder-name)
- "`."))))
+ (send-to this :spew-fail-to-move-up-error))))
+
+ ;; Private
+ :holder-name-above
+ (fn []
+ (let [symbol-above (method-holder-symbol-above (send-to this :holder-name))]
+ (find-containing-holder-symbol symbol-above (send-to this :name))))
+
+ :spew-fail-to-move-up-error
+ (fn []
+ (throw (Error. (str "No superclass method `" (send-to this :name)
+ "` above `" (send-to this :holder-name)
+ "`."))))
}
{})
-(def tentative-message
- (fn [target name args]
- (let [initialize (get (held-methods 'Message) :add-instance-values)
+(def send-to-Message-new ;; Supposed to remind you of (send-to Message :new ...)
+ (fn [target name args holder-name]
+ (let [initializer (get (held-methods 'ActiveMessage) :add-instance-values)
;; At this moment, `this` is still bound to the ;; <<==
;; sender of the message. ;; <<==
sender this] ;; <<==
- (binding [this (basic-object 'Message)]
- (initialize :name name
- :holder-name (find-containing-holder-symbol
- (:__left_symbol__ target)
- name)
- :args args
- :target target
- :sender sender))))) ;; <<==
-
+ (binding [this (basic-object 'ActiveMessage)]
+ (initializer :name name
+ :holder-name holder-name
+ :args args
+ :target target
+ :sender sender))))) ;; <<==
;;; Exercise 7
(def ^:dynamic message nil)
(send-to Klass :new
- 'Message 'Anything
+ 'ActiveMessage 'Anything
{
:add-instance-values
(fn [& key-value-pairs]
@@ -207,14 +190,14 @@
:args (fn [] (:args this))
:target (fn [] (:target this))
:sender (fn [] (:sender this))
- :previous (fn [] (:previous this)) ;; <<==
- :super-count (fn [] (:super-count this)) ;; <<==
-
- :trace ;; <<==
+ :previous (fn [] (:previous this)) ;; <<==
+ :super-count (fn [] (:super-count this)) ;; <<==
+
+ :trace ;; <<==
(fn []
;; Note that I decided, just to make things more varied, that
;; the work of turning a linked list of message lists into a
- ;; sequence should be a class method of `Message`.
+ ;; sequence should be a class method of `ActiveMessage`.
(let [raw-results (send-to (send-to this :class) :message-trace-to-sequence this)
formatted-results (map (fn [result]
(select-keys result
@@ -224,29 +207,28 @@
:move-up
(fn []
- (let [holder-name (send-to this :find-containing-holder-symbol)]
- (if holder-name
+ (let [holder-name (send-to this :holder-name-above)]
+ (if holder-name
(assoc this
:holder-name holder-name
- :previous this
+ :previous this
:super-count (inc (send-to this :super-count))) ;; <<==
- (send-to this :no-message-to-move-up-to))))
-
- :find-containing-holder-symbol
- (fn []
- (find-containing-holder-symbol (send-to this :next-holder-up)
- (send-to this :name)))
-
- :next-holder-up
- (fn []
- (method-holder-symbol-above (send-to this :holder-name)))
-
- :no-message-to-move-up-to
- (fn []
- (throw (Error. (str "No superclass method `" (send-to this :name)
- "` above `" (send-to this :holder-name)
- "`."))))
+ (send-to this :spew-fail-to-move-up-error))))
+
+ ;; Private
+ :holder-name-above
+ (fn []
+ (let [symbol-above (method-holder-symbol-above (send-to this :holder-name))]
+ (find-containing-holder-symbol symbol-above (send-to this :name))))
+
+ :spew-fail-to-move-up-error
+ (fn []
+ (throw (Error. (str "No superclass method `" (send-to this :name)
+ "` above `" (send-to this :holder-name)
+ "`."))))
}
+
+ ;; Class methods
{
:message-trace-to-sequence ;; <<==
(fn [final-message]
@@ -258,20 +240,17 @@
})
-(def tentative-message
- (fn [target name args]
- (let [initialize (get (held-methods 'Message) :add-instance-values)
- previous message ;; <<==
- sender this]
- (binding [this (basic-object 'Message)]
- (initialize :name name
- :holder-name (find-containing-holder-symbol
- (:__left_symbol__ target)
- name)
- :args args
- :target target
- :sender sender
- :previous previous ;; <<==
- :super-count 0))))) ;; <<==
-
+(def send-to-Message-new ;; Supposed to remind you of (send-to Message :new ...)
+ (fn [target name args holder-name]
+ (let [initializer (get (held-methods 'ActiveMessage) :add-instance-values)
+ previous *active-message* ;; <<==
+ sender this]
+ (binding [this (basic-object 'ActiveMessage)]
+ (initializer :name name
+ :holder-name holder-name
+ :args args
+ :target target
+ :sender sender
+ :previous previous ;; <<==
+ :super-count 0))))) ;; <<==
Oops, something went wrong.

0 comments on commit f887395

Please sign in to comment.