[Gsll-devel] Introducing "Grid Structured Data"
Tamas Papp
tkpapp at gmail.com
Sun Jan 17 08:17:41 UTC 2010
On Sat, 16 Jan 2010, Mirko Vukovic wrote:
> How would one handle this:
>
> Suppose I have an vector V of containers C (in this particular case, these
> are objects of same type that hold data in matrix form)
> v=#(c0 c1 c2 c3 c4)
>
> I extract a matrix from a C like (m c1), and I want the (aref (m C) 0 5).
>
> Can xarray let me do (magic-command v 2 0 5) to extract the 0 5 element from
> the matrix in (aref v 2) (in this case, c2)?
>
> I know I can write a function to do that. But does xarray provide a
> built-in unified mechanism to allow rows and columns of the stored matrix?
> Maybe that is part of your response above?
xref is a generic function, dispatching on its first argument. So if
you have a vector, that is a subtype of array, so xref will call the
method for arrays, which will just signal an error for the extra
subscripts.
You can deal with this by wrapping your vector in a class, and then
dispatching on that, defining a method for xref that will use the
extra subscripts to look up elements in the container.
Of course, one could change the semantics and deal with extra
subscripts by looking stuff up recursively. At first glance, that
looks like a "clever trick", ie something appealing in a particular
situation, with the possibility of leading to subtle bugs in general.
R is full of these. Generally, I am very hostile to "clever"
semantics: I always prefer to see an error, instead of the interface
guessing that I meant something else.
If you still want magic-command, it can be done like this (untested):
(defun xref* (object &rest subscripts)
(let ((rank (xrank object)))
(if (< rank (length subscripts))
(apply #'xref* (apply #'xref object (subseq subscripts 0 rank))
(nthcdr rank subscripts))
(xref object subscripts))))
Tamas
More information about the gsll-devel
mailing list