-
-
Save sjl/3750572 to your computer and use it in GitHub Desktop.
defrec, rhymes with vec, because you can nth it
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 defrec [name args & body] | |
(let [indexed-args (interleave (iterate inc 0) args)] | |
`(defrecord ~name ~args | |
clojure.lang.Indexed | |
(nth [_# i#] | |
(case i# | |
~@indexed-args | |
(throw (IndexOutOfBoundsException.)))) | |
(nth [_# i# default#] | |
(case i# | |
~@indexed-args | |
default#)) | |
~@body))) |
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
(defrec Point [x y]) | |
(let [p (->Point 10 20) | |
[x y] p] | |
(println y x)) | |
;=> 20 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment