Macro: WITH-COLLECTOR

Documentation

Bind NAME to a collector function and execute BODY. If FROM-END is true the collector will actually be a pusher, (see MAKE-PUSHER), otherwise NAME will be bound to a collector, (see MAKE-COLLECTOR).

Source

(defmacro with-collector ((name &optional initial-value from-end) &body body)
  "Bind NAME to a collector function and execute BODY. If
  FROM-END is true the collector will actually be a pusher, (see
  MAKE-PUSHER), otherwise NAME will be bound to a collector,
  (see MAKE-COLLECTOR)."
  (with-unique-names (collector)
    `(let ((,collector ,(if from-end
                            `(make-pusher ,initial-value)
                            `(make-collector ,initial-value))))
       (flet ((,name (&rest items)
                (if items
                    (dolist (i items)
                      (funcall ,collector i))
                    (funcall ,collector))))
         ,@body))))
Source Context