;;; along with this program; if not, write to the Free Software Foundation,
;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-(defpackage #:mdw.collect
+(defpackage #:collect
(:use #:common-lisp #:mdw.base)
- (:export #:collecting #:with-collection #:collect))
-(in-package mdw.collect)
+ (:export #:collecting #:with-collection #:collect #:collect-tail))
+(in-package collect)
(eval-when (:compile-toplevel :load-toplevel)
(defvar *collecting-anon-list-name* (gensym)
(defun make-collector ()
(let ((c (cons nil nil)))
(cons c c))))
+
(defmacro collecting (vars &body body)
"Collect items into lists. The VARS are a list of collection variables --
their values are unspecified, except that they may be passed to `collect' and
`(let ,(mapcar (lambda (v) `(,v (make-collector))) vars)
,@body
(values ,@(mapcar (lambda (v) `(cdar ,v)) vars))))
+
(defmacro with-collection (vars collection &body body)
"Collect items into lists VARS according to the form COLLECTION; then
evaluate BODY with VARS bound to those lists."
,(listify vars)
(collecting ,vars ,collection)
,@body))
+
(defmacro collect (x &optional (name *collecting-anon-list-name*))
"Add item X to the `collecting' list NAME (or *collecting-anon-list-name*
by default)."
`(let ((,tmp (cons ,x nil)))
(setf (cddr ,name) ,tmp)
(setf (cdr ,name) ,tmp))))
+
(defmacro collect-tail (x &optional (name *collecting-anon-list-name*))
"Make item X be the tail of `collecting' list NAME (or
*collecting-anon-list-name* by default). It is an error to continue trying