Last active
August 29, 2015 14:27
-
-
Save tuturto/486c4ff24b94248a2dfe to your computer and use it in GitHub Desktop.
set-attributes
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
(defmacro set-attributes [&rest attributes] | |
`(do ~@(genexpr `(setattr self ~x ~x) [x attributes]))) | |
(defmacro set-attributes [&rest attributes] | |
`(do ~@(genexpr `(setv self.~x ~x) [x attributes]))) | |
(defn --init-- [self foo bar baz] | |
(set-attributes foo bar baz)) | |
-> | |
(defn --init-- [self foo bar baz] | |
(do | |
(setv self.foo foo) | |
(setv self.bar bar) | |
(setv self.baz baz))) | |
--- | |
(defmacro set-attributes [&rest attributes] | |
`(do ~@(genexpr `(setattr self (name ~x) ~x) [x attributes]))) | |
(defclass Cat [] | |
[[--init-- (fn [self cat-name size] | |
(set-attributes cat-name size))]]) | |
(dir (Cat "miisa" "huge")) | |
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'huge', 'miisa'] | |
-- working one -- | |
=> (defmacro set-attributes [&rest attributes] | |
=> (let [[code (list-comp (, x (name x)) [x attributes])]] | |
=> `(do ~@(list-comp `(setattr self ~(second x) ~(first x)) [x code])))) | |
=> (setv miisa (Cat "miisa" "huge")) | |
=> miisa.name | |
"miisa" | |
=> miisa.size | |
"huge" | |
-- even better working one -- | |
(defmacro set-attributes [&rest attributes] | |
`(do ~@(genexpr `(setv (. self ~x) ~x) [x attributes]))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment