Skip to content

Instantly share code, notes, and snippets.

@timsgardner
Last active August 29, 2015 14:14
Show Gist options
  • Save timsgardner/1181fb86151e602c3f94 to your computer and use it in GitHub Desktop.
Save timsgardner/1181fb86151e602c3f94 to your computer and use it in GitHub Desktop.
array-mac
(defmacro array-mac [t & elements]
(let [^Type tt (resolve t)
atypesym (symbol (str (.FullName tt) "[]"))
asym (gensym "array_")
setexprs (->> elements
(map-indexed
(fn [i x]
`(aset ~asym ~i ~x))))]
`(let [~(with-meta asym {:tag atypesym}) (make-array ~t ~(count elements))]
~@setexprs
~asym)))
@timsgardner
Copy link
Author

For when you find yourself writing something like

(into-array Vector3 [a b c])

@timsgardner
Copy link
Author

(array-mac Vector3 a b c)

expands to

(clojure.core/let
 [^UnityEngine.Vector3[] array_669 (clojure.core/make-array Vector3 3)]
 (clojure.core/aset array_669 0 a)
 (clojure.core/aset array_669 1 b)
 (clojure.core/aset array_669 2 c)
 array_669)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment