Hi guys, I am working through the pre-release second edition of Joy of 
Clojure's section on multi-methods (section 9.2.~ : pg. 313), and am 
getting different outputs from what they have printed in the book. I could 
just skip over it, but I really want to understand this stuff. Could 
someone tell me how to get the *(compile-cmd osx) *call at the end of the 
code to produce the expected output:  *"/usr/bin/gcc" *? I have commented 
out redundancy and moved a couple of lines for readability. Thanks in 
advance! 

J

> (ns joy.udp
>   (:refer-clojure :exclude [get]))
>
> (defn beget [this proto]
>   (assoc this ::prototype proto))
>
> (defn get [m k]
>   (when m
>     (if-let [[_ v] (find m k)]
>       v
>       (recur (::prototype m) k))))
>
> (def put assoc)
>
> ;;;;;;; compiler
> (defmulti compiler :os)
> (defmethod compiler ::unix [m] (get m :c-compiler))
> (defmethod compiler ::osx [m] (get m :llvm-compiler))
>
> (def clone (partial beget {}))
>
 

> (def unix {:os ::unix, :c-compiler "cc", :home "/home", :dev "/dev"})
>
 

> (def osx (-> (clone unix)
>              (put :os ::osx)
>              (put :llvm-compiler "clang")
>              (put :home "/Users")))
>
> ;;;;;;; home
> (defmulti home :os)
> (defmethod home ::unix [m] (get m :home))
> (defmethod home ::bsd [m] "/home")
>
 

> ;; the error on the call to (home osx) is contingent upon toggling the 
> following lines. 

;(derive ::osx ::unix)
> ;(derive ::osx ::bsd)
> (prefer-method home ::unix ::bsd)
> ;(remove-method home ::bsd)   
> (derive (make-hierarchy) ::osx ::unix) 
>
> ;;;;;;; compile-cmd
> (defmulti compile-cmd (juxt :os compiler))
>
> (defmethod compile-cmd [::osx "gcc"] [m]
>   (str "/usr/bin/" (get m :c-compiler)))
>
> (defmethod compile-cmd :default [m]
>   (str "Unsure where to locate " (get m :c-compiler)))
> ;;;;;;;;;;;;;;;;;;;;;;
>
> (home osx) 
>
;=> java.lang.IllegalArgumentException: No method in multimethod 'home' for 
> dispatch value: :joy.udp/osx …
>
 ;; Should be: ;=> "/Users"


(compile-cmd osx)

;=> "Unsure where to locate cc"     

 ;; Should be: ;=> "/usr/bin/gcc"

 

> (compile-cmd unix)

;=> "Unsure where to locate cc"

;; this is the expected output 

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to