Skip to content

Instantly share code, notes, and snippets.

@rauhs
Last active May 25, 2021 15:14
Show Gist options
  • Save rauhs/d49d6f8a6f5fbb8230647c5b2ac210b2 to your computer and use it in GitHub Desktop.
Save rauhs/d49d6f8a6f5fbb8230647c5b2ac210b2 to your computer and use it in GitHub Desktop.
(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 arfor
"Like for but eagerly builds a JS array.
Usually for react consumption."
[[item coll] & body]
`(reduce (fn [neue# ~item]
(.push neue# (html ~@body))
neue#)
(cljs.core/array) ~coll))
(defmacro arifor
"Like arfor also gives the index. Syntax is like this to allow cursive to give variable
highlighting. Note: not destructing is happening. A fast reduce with an index is done.
(arifor [[idx item] items]
[:div {} idx \":\" item])"
[[[idx item] coll] & body]
`(let [coll# ~coll
neue# (cljs.core/array)]
(reduce (fn [~idx ~item]
(.push neue# (html ~@body))
(inc ~idx))
0 coll#)
neue#))
@rauhs
Copy link
Author

rauhs commented Mar 25, 2017

Will need a html macro which can be like:

(defmacro html
  [body]
  (sablono.compiler/compile-html body))

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