Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
doseq-indexed
;; Example:
;; (doseq-indexed idx [name names]
;; (println (str idx ". " name)
(defmacro doseq-indexed [index-sym [item-sym coll] & body]
`(let [idx-atom# (atom 0)]
(doseq [~item-sym ~coll]
(let [~index-sym (deref idx-atom#)]
~@body
(swap! idx-atom# inc)))))
@AlexBaranosky

This comment has been minimized.

Copy link
Owner Author

commented Mar 12, 2016

A lot better:

(defmacro doseq-indexed [index-sym [item-sym coll] & body]
  `(doseq [[~index-sym ~item-sym] (map list (range) ~coll)]
     ~@body))
@viebel

This comment has been minimized.

Copy link

commented May 9, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.