(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
It looks like it expects the keyword :osx, not the symbol osx. Could
that be the issue?
I kind of feel it might be a typo in the compile-cmd function, cause instead of a sybol it is listing a vector with a name-spaced symbol and a string: [::osx "gcc"]. I think this form might have to do something with juxt in (defmulti compile-cmd (juxt :os compiler)) . The real problem though is the last two calls to compile-cmd. I've been messing with it for a couple of days so any help there would be well appreciated.
Thanks
J
Isn't there a core function/macro where I can derive and set hierarchy all at once?