chiark / gitweb /
base: New macro setf-default.
[lisp] / collect.lisp
index 946ba916257b253cb413c300eac350c25c1a7075..359780c5363fbb831fac52115a44256f180c0d36 100644 (file)
@@ -45,7 +45,7 @@ (defmacro collecting (vars &body body)
        ((atom vars) (setf vars (list vars))))
   `(let ,(mapcar (lambda (v) `(,v (make-collector))) vars)
      ,@body
-     (values ,@(mapcar (lambda (v) `(cdr ,v)) vars))))
+     (values ,@(mapcar (lambda (v) `(the list (cdr ,v))) vars))))
 
 (defmacro with-collection (vars collection &body body)
   "Collect items into lists VARS according to the form COLLECTION; then
@@ -71,4 +71,19 @@ (defmacro collect-tail (x &optional (name *collecting-anon-list-name*))
      (setf (cdar ,name) ,x)
      (setf (car ,name) nil)))
 
+(defmacro collect-append (list &optional (name *collecting-anon-list-name*))
+  "Append LIST to the tail of `collecting' list NAME.  This obviously
+   involves copying LIST."
+  (with-gensyms item
+    `(dolist (,item ,list)
+       (collect ,item ,name))))
+
+(defmacro collect-nconc (list &optional (name *collecting-anon-list-name*))
+  "Attach LIST to the tail of `collecting' list NAME.  This will involve
+   destroying LIST if anything else gets collected afterwards."
+  (let*/gensyms list
+    `(when ,list
+       (setf (cdar ,name) ,list)
+       (setf (car ,name) (last ,list)))))
+
 ;;;----- That's all, folks --------------------------------------------------