Created
October 13, 2009 19:40
-
-
Save anonymous/209477 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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