Re: Iterating over generic sequences.
- From: Pascal Costanza <pc@xxxxxxxxx>
- Date: Wed, 26 Sep 2007 21:23:08 +0200
Pillsy wrote:
What do you use to iterate over a generic sequence (in the manner of
quite a few of the "library" functions like FIND-IF)? The major
possibilities seem to be:
1. Using ELT with LOOP or DOTIMES (which seems needlessly inefficient,
especially for lists). The upside is that it's very simple.
2. Using MAP with the appropriate function. This is what I usually do.
It's a little less clear, and for all I know it really isn't that much
more efficient than the ELT solution.
3. Using something more general with closures and LOOP or DO, like so:
(defun do-stuff-with-sequence (sequence)
(let ((iterator
(etypecase sequence
(list
#'(lambda ()
(pop sequence)))
(vector
(let ((i 0)
(length (length sequence)))
#'(lambda ()
(if (= i length)
nil
(prog1
(aref sequence i)
(incf i)))))))))
(loop
:for elt := (funcall iterator)
:while elt
:do (stuff elt))))
(defgeneric do-stuff-with-sequence (sequence)
(:method ((sequence vector))
(loop for element across sequence
do (stuff element)))
(:method ((sequence list))
(loop for element in sequence
do (stuff element))))
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
.
- References:
- Iterating over generic sequences.
- From: Pillsy
- Iterating over generic sequences.
- Prev by Date: Re: Trying to learn Common Lisp
- Next by Date: Re: Learning LISP from scratch
- Previous by thread: Iterating over generic sequences.
- Next by thread: Re: Iterating over generic sequences.
- Index(es):
Relevant Pages
|