Skip to content
This repository was archived by the owner on Dec 13, 2025. It is now read-only.

Commit f99ba29

Browse files
committed
collecting: collector-pop, collector-empty-p
1 parent 25c7508 commit f99ba29

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ is equivalent to
403403

404404
except that no new list structure is created. See `collector-contents` for a function which does not update the tail pointer.
405405

406+
**`collector-empty-p`** returns true if its argument is a collector which is empty (note it doesn't perform any check that the argument actually is a valid collector object).
407+
408+
**`pop-collector`** returns the first element of a collector, and alters it so that the second element is now the first element. It can be used to use collectors like queues.
409+
406410
### Notes on explicit collectors
407411
Surprising things can happen if you share a single list between more than one collector without copying it:
408412

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10.4.0
1+
10.5.0

collecting.lisp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
#:collector-contents
3939
#:collect-into
4040
#:nconc-collectors
41-
#:nconc-collector-onto))
41+
#:nconc-collector-onto
42+
#:pop-collector
43+
#:collector-empty-p))
4244

4345
(in-package :org.tfeb.hax.collecting)
4446

@@ -305,14 +307,13 @@ This is the closest equivalent to Interlisp's TCONC."
305307
;; Note unlike APPEND it makes no sense to call this with no
306308
;; collectors at all: what should it return in that case (perhaps a
307309
;; new collector)?
308-
(declare (dynamic-extent collectors))
309-
310310
"Destructively concatenate one or more collectors, returning the first
311311
312312
All the collectors share a tail pointer after this is done, while
313313
their head pointers point at appropriate points on the NCONCed list.
314314
You can then collect more into any one of them but this will make the tail
315315
pointers of all the others junk."
316+
(declare (dynamic-extent collectors))
316317
(if (null collectors)
317318
collector
318319
(labels ((ncc (c a more)
@@ -341,3 +342,15 @@ Return the collector."
341342
(setf (cdr (cdr collector)) onto
342343
(cdr collector) (last onto)))
343344
collector)
345+
346+
(defun pop-collector (c)
347+
"Pop the first value from C
348+
349+
If C is empty, then this will return NI"L
350+
(prog1 (pop (car c))
351+
(when (null (car c))
352+
(setf (cdr c) nil))))
353+
354+
(defun collector-empty-p (c)
355+
"Return true if the collector C is empty"
356+
(null (car c)))

test/test-collecting.lisp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@
7171
(collect-into c 0)
7272
(collect-into (nconc-collector-onto c l) 4)
7373
(is equal '(0 1 2 3 4) (collector-contents c))
74-
(is equal '(1 2 3 4) l)))
74+
(is equal '(1 2 3 4) l))
75+
(let ((c (make-collector)))
76+
(true (collector-empty-p c))
77+
(collect-into c 1)
78+
(false (collector-empty-p c))
79+
(collect-into c 2)
80+
(is equal '(1 2) (collector-contents c))
81+
(is eql 1 (pop-collector c))
82+
(false (collector-empty-p c))
83+
(collect-into c 3)
84+
(is equal '(2 3) (collector-contents c))
85+
(is eql 2 (pop-collector c))
86+
(is eql 3 (pop-collector c))
87+
(true (collector-empty-p c))))
7588

7689
(test "org.tfeb.hax.collecting" :report 'summary)

0 commit comments

Comments
 (0)