Exporting LENGTH=1 and LENGTH=2 may be debateable, the only use case is
the one Andreas Fuchs mentioned, i.e. as arguments to FIND-IF &c.
i've got this recorded and ready to be pushed. unless soemone has
additional comments, i'll push it eventually.
(note: i've not included the debatable length=1/2)
--
attila
Sat Mar 1 10:01:54 CET 2008 attila.lendvai@gmail.com
* Added length>, WARNING: renamed sequence-of-length-p to length=
(Based on code by Tobias C. Rittweiler)
diff -rN -u old-alexandria/package.lisp new-alexandria/package.lisp
--- old-alexandria/package.lisp 2008-03-01 10:02:20.000000000 +0100
+++ new-alexandria/package.lisp 2008-03-01 10:02:21.000000000 +0100
@@ -98,7 +98,8 @@
#:random-elt
#:removef
#:rotate
- #:sequence-of-length-p
+ #:length=
+ #:length>
#:shuffle
#:starts-with
#:starts-with-subseq
diff -rN -u old-alexandria/sequences.lisp new-alexandria/sequences.lisp
--- old-alexandria/sequences.lisp 2008-03-01 10:02:20.000000000 +0100
+++ new-alexandria/sequences.lisp 2008-03-01 10:02:21.000000000 +0100
@@ -1,5 +1,9 @@
(in-package :alexandria)
+;; Make these inlinable by declaiming them INLINE here and some of them
+;; NOTINLINE at the end of the file.
+(declaim (inline copy-sequence length= length>))
+
(defun rotate-tail-to-head (sequence n)
(declare (type (integer 1) n))
(if (listp sequence)
@@ -113,9 +117,12 @@
(list (null sequence))
(sequence (zerop (length sequence)))))
-(defun sequence-of-length-p (sequence length)
+(defun length= (sequence length)
"Return true if SEQUENCE is a sequence of length LENGTH. Signals an error if
SEQUENCE is not a sequence. Returns FALSE for circular lists."
+ (declare (type array-index length)
+ (inline length)
+ (optimize speed))
(etypecase sequence
(null
(zerop length))
@@ -123,11 +130,32 @@
(let ((n (1- length)))
(unless (minusp n)
(let ((tail (nthcdr n sequence)))
- (and tail (null (cdr tail)))))))
+ (and tail
+ (null (cdr tail)))))))
+ (simple-vector
+ (= length (length sequence)))
+ (vector
+ (= length (length sequence)))
(sequence
(= length (length sequence)))))
-(declaim (inline copy-sequence))
+(defun length> (sequence n)
+ "Returns non-nil if (> (length SEQUENCE) N)."
+ (declare (inline length)
+ (optimize speed)
+ (type array-index n))
+ (etypecase sequence
+ (list
+ (and (>= n 0)
+ (nthcdr n sequence)
+ t))
+ (simple-vector
+ (> (length sequence) n))
+ (vector
+ (> (length sequence) n))
+ (sequence
+ (> (length sequence) n))))
+
(defun copy-sequence (type sequence)
"Returns a fresh sequence of TYPE, which has the same elements as
SEQUENCE."
@@ -395,3 +423,5 @@
(setf (bit mask i) 0))))))
(derange start size)
sequence)))
+
+(declaim (notinline length= length>))