I have a macro below that iterates along bits in an integer. I would like to integrate the collect capability of the loop like this:
(loop for x in '(a b c d e)
for y in '(1 2 3 4 5)
collect (list x y) )
How should I modify the macro below to accomplish the above?
(defmacro do-bits ((var x) &rest body)
"Evaluates [body] forms after binding [var] to each set bit in [x]"
(let ((k (gensym)))
`(do ((,k ,x (logand ,k (1- ,k))))
((= ,k 0))
(let ((,var (logand ,k (- ,k))))
,@body))))
here’s a simple macro
with-collectorthat should do the trick:it uses the name
collectby default:but you can use another name if you like (e.g. for nesting or resolving a symbol conflict)
to integrate it with your macro, just wrap the
doform:and
collectwill be available in the body: