From: Harold
Subject: Novice List to Vector question
Date: 
Message-ID: <1125692144.913285.201340@g47g2000cwa.googlegroups.com>
I have some code I'm changing to use vectors instead of lists. Since it
can be a pain to change #'nth to #'aref in all the appropriate places,
I wanted to use a generic accessor for a location in a document:

(defgeneric elem (location collection)
  (:documentation "Look up the element at a location in the
collection."))

(defgeneric (setf elem) (value location collection)
  (:documentation "Set the value of an element at a location in a
collection."))

Then I implemented this for lists, arrays/vectors, hash tables, slots
in classes and symbols in packages. My question is, does everyone
reinvent the wheel on this? And I know it is probably an older than
dirt question, but since generic functions were in the ANSI spec, why
aren't more of the built-in functions generic?

Yeah, I also know about #'elt, but I want something that can handle
multi-dimensional arrays, so I have one function for getting an element
in a location of a collection.

Also, I'm in the process of changing the signature to

(defgeneric elem (collection &rest location))

so that I can get that nice (apply #'elem multi-dimensional-array x y
z) behavior I've been reading about lately on c.l.l. Will that
sacrifice specialization on the location? e.g. my method for lists is
currently

(defmethod elem ((location number) (collection list))
  "Elem for lists."
  (nth location collection))

will I still be able to limit location to numbers when it is the
catch-all &rest?
From: Wade Humeniuk
Subject: Re: Novice List to Vector question
Date: 
Message-ID: <iH2Se.253004$on1.30608@clgrps13>
Harold wrote:

> 
> Then I implemented this for lists, arrays/vectors, hash tables, slots
> in classes and symbols in packages. My question is, does everyone
> reinvent the wheel on this? And I know it is probably an older than
> dirt question, but since generic functions were in the ANSI spec, why
> aren't more of the built-in functions generic?
> 

No I do not not for a couple of reasons,

1)  For structured data I use structures and classes, which have
     named accessors for the various "slots".  Thus I use calls
     like (position obj) instead of the more obtuse (elem obj 2), which
     if I used an array to store structured data I would have to resort
     to.

2)  In the case for ordered data whose slots can not named easily, like
     a 2D array of pixels, what I will do is create helper functions like

     (defun pixel (obj x y) (aref (pixels obj) x y))

     Then if I change how pixels are stored I just update the pixel function.
     Then in the code the meaningful accesses to the pixels becomes
     (pixel pixmap i j).

So in general I prefer to use more meaningful (higher level naming) coding.
Thus the problem you are refering to hardly ever is a real problem for me.

As for why are there are not more built-in generic methods.  Its for
the writers of the compilers where they can depend on various functions
being specific for various types, and thus optimizations can be performed.

Wade