Skip to content

Instantly share code, notes, and snippets.

@rauhs
Last active November 10, 2017 10:43
Show Gist options
  • Select an option

  • Save rauhs/f71f30c195bc8bebc400c2f7275970ab to your computer and use it in GitHub Desktop.

Select an option

Save rauhs/f71f30c195bc8bebc400c2f7275970ab to your computer and use it in GitHub Desktop.
(defmacro amap-inplace-new
"Maps an expression across an array a, using an index named idx, and
element el:
(amap-inplace [[idx el] arr]
[idx el])"
[[[idx el] arr] & body]
`(let [a# ~arr]
(dotimes [~idx (alength a#)]
(let [~el (aget a# ~idx)]
(aset a# ~idx (do ~@body))))))
(defmacro afor
"Like for but eagerly builds a JS array.
Usually for react consumption."
[[item coll] & body]
`(reduce (fn [neue# ~item]
(.push neue# (do ~@body))
neue#)
(cljs.core/array) ~coll))
(defmacro set!!
([x v]
(list 'js* "~{} = ~{}" x v))
([x a0 v]
(list 'js* "~{} = ~{}" (list '.. x a0) v))
([x a0 a1 v]
(list 'js* "~{} = ~{}" (list '.. x a0 a1) v))
([x a0 a1 a2 v]
(list 'js* "~{} = ~{}" (list '.. x a0 a1 a2) v)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment