Skip to content

Instantly share code, notes, and snippets.

Created October 13, 2009 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/209477 to your computer and use it in GitHub Desktop.
Save anonymous/209477 to your computer and use it in GitHub Desktop.
(ns prototype)
;;; prototype based "type" system, following the Uniform access principle (Clojure)
(defn create-prototype [data]
(fn
([kw]
(let [member (data kw)]
(if (fn? member) (member data) member)))
([kw & more]
(let [member (data kw)]
(apply member (cons data more))))))
(defn mutate [data & more] (create-prototype (conj data (apply hash-map more))))
(def prototype-initial-data {:mutate mutate})
(def prototype (create-prototype prototype-initial-data))
;(comment "Usage"
(do
(defn hungarian-fullName [this] (str (this :surName) " " (this :givenName)))
(defn fullName [this] (str (this :givenName) " " (this :surName)))
(def western-name (prototype :mutate :surName "N/A" :givenName "N/A" :fullName fullName))
(def hungarian-name (western-name :mutate :fullName hungarian-fullName))
(def a (western-name :mutate :givenName "John" :surName "von Neumann" ))
(def b (hungarian-name :mutate :givenName "Janos" :surName "Neumann" ))
(println (a :fullName))
(println (b :fullName))
(println (b :givenName))
)
;)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment