1. RE: On indexes
- Posted by Matthew Lewis <matthewwalkerlewis at YAHOO.COM> Aug 23, 2002
- 415 views
-----Original Message----- From: christian.cuvier at education.gouv.fr [mailto:christian.cuvier at education.gouv.fr] > But there may be a problem with variable depth indexing. Assume we want to manipulate > arbitrary trees in Euphoria. Trees are two-terms sequences: one object as root node > content and a sequence of 0 or more trees (the subtrees originating from root). > Accessing an individual statement using ordinary indexes requires to know, before run > time, its depth. Since generally we don't, the indexing scheme should be dynamic, so > it must be a sequence. How to get or set an arbitrary tree subtree or element (a 0-depth > node), given a tree, an index sequence and a value? > What about setting an element? > The way I found is to duplicate the tree with the modified element and return it. It seems > a terrible waste of machine cycles and memory. If somebody knows better, I'd appreciate. > If the interpreter is so smart that it does not really duplicate the structure, this rare > feature needs be more thoroughly documented. This came up a while back. After some help from Jiri, I came up with the following iterative fetch and recursive store functions. It should only copy the most deeply nested sequence within b. Mostly, only pointers are copied (internally). Pass c = 0 to simply assign b = a. c = {1,0} = {1}. I use this in my database code to fetch and store nested fields. -- return a[b...] function seq_fetch(object a, sequence b) for i = 1 to length(b) do a = a[b[i]] end for return a end function -- store a in b at subcript c function seq_store(object a, object b, object c) integer len if atom(c) then c = {c} end if len = length(c) -- now it will insert a new element! if c[1] = -1 or length(b) + 1 = c[1] then return b & { a } elsif len > 1 then -- recursively go into the sequence b[c[1]] = seq_store(a, b[c[1]], c[2..len] ) return b end if -- get the index c = c[1] if c then b[c] = a else b = a end if return b end function Matt Lewis
2. RE: On indexes
- Posted by rforno at tutopia.com Aug 23, 2002
- 405 views
Derek: Maybe I was the one requesting this feature.... But if I remember well, what I asked was that: x[{1, 5, 3}] was {x[1], x[5], x[3]}. I think the two approaches are worth considering, with different notations. The second one is addressed to in one of the functions in my genfunc package. ----- Original Message ----- From: Derek Parnell <ddparnell at bigpond.com> Subject: Re: On indexes > > ----- Original Message ----- > From: christian.cuvier at education.gouv.fr > To: EUforum > Sent: Friday, August 23, 2002 9:15 PM > Subject: On indexes > > >I am not asking for a t[index]=setval syntax to be available > >- too much specialized-. > > > >Hav a good week-end. > > > However, it has been requested by at least one person that sequences support > indexes that are also sequences. For example: > > a = t[{3,5,2}] > > would be equivalent to ... > > a = t[3][5][2] > > The advantage would be that it makes handling arbitary depth sequences with > generic code more easier. Currently, we have to specify in the code, the > same number of [] as there are depths. If we could code a single [] that > takes a sequence as its index value, we could then control the depth > references just by altering the number of elements in the index sequence. > > It makes writing generic code in libraries much easier. > > ---------------- > cheers, > Derek Parnell > > > >