[alexandria-devel] length=1

Attila Lendvai attila.lendvai at gmail.com
Fri Feb 22 14:57:45 UTC 2008


>  On Feb 22, 2008, at 12:30, Nikodemus Siivola wrote:
>  > How about LENGTH= ?
>  >
>  > ;; stupid version
>  > (defun length= (n seq)
>  >  (= n (length seq)))
>
>  I think the idea is to have something you can use as an argument to
>  functions like find-if. My own solution when I wanted that was
>  functions returning closures:

my need comes from cl-rdbms: it can be configured to return resultsets
both as vectors and lists, and code dealing with them is full of calls
to FIRST, SECOND, LENGTH=1 and friends that should work transparently
on both lists and vectors.

for a short moment i wanted to suggest to shadow CL:FIRST and friends
in the alexandria package with unexported versions that work on
generic sequences and let users :shadowing-import-from as/when they
need them. that way i could forget FIRST*. but then i realized that
it's kind of like a blasphemy to talk about this, so i only
hypothetically mention this idea now, hoping that i won't be thrown
with stones... :) so, could this survive without any vetos? there's
already FIRST-ELT in sequences.lisp...

but to be specific about length=1:

diff -rN -u old-alexandria/sequences.lisp new-alexandria/sequences.lisp
--- old-alexandria/sequences.lisp       2008-02-22 15:53:06.000000000 +0100
+++ new-alexandria/sequences.lisp       2008-02-22 15:53:06.000000000 +0100
@@ -107,9 +107,17 @@
     (list (null sequence))
     (sequence (zerop (length sequence)))))

+(declaim (inline length=1))
+(defun length=1 (sequence)
+  (declare (inline sequence-of-length-p)
+           (optimize speed))
+  (sequence-of-length-p sequence 1))
+
 (defun sequence-of-length-p (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)
+           (optimize speed))
   (etypecase sequence
     (null
      (zerop length))

-- 
 attila



More information about the alexandria-devel mailing list