Re: Possible feature for new Euphoria-version
- Posted by "Derek Parnell" <ddparnell at bigpond.com> Jan 09, 2004
- 389 views
----- Original Message ----- From: "Carl W." <euphoria at cyreksoft.yorks.com> To: <EUforum at topica.com> Subject: Re: Possible feature for new Euphoria-version > > > Tommy Carlier wrote: > > > Options for seq[seq]: > > > > 1. s[{a, b, c}] = s[a][b][c] -- recursive indices > > 2. s[{a, b, c}] = { s[a], s[b], s[c] } -- direct indices > > 3. s["abc"] = s[VALUES][ find("abc", s[KEYS]) ] -- associative list > > 1: Simpler than you might think: > > -- Get 's' from somewhere, then... > params = {a, b, c} > t = s > for i = 1 to length(params) do > t = t[params[i]] > end for > -- t is now s[a][b][c] Yes, this is almost exactly how I coded it in my support library. The hard part was putting it as a SET operation. S[{a,b,c}] = d This needs recursion I think. Well that's how I've got it coded. Here is code from my 'basic.e' library -- Returns an element from source. Index is a list of subscripts -- Example: rc = seqfetch({"abc","def"}, {2,1}) -- --> rc is 'd' -- Example: rc = seqfetch({"abc","def"}, {1,2}) -- --> rc is 'b' ---------------------------------------- global function seqfetch(object piSource, object piIndex) ---------------------------------------- -- fetch from piSource using index sequence piIndex for i=1 to length(piIndex) do if atom(piSource) then exit end if piSource = piSource[piIndex[i]] end for return piSource end function -- Returns an updated sequence. Index is a list of subscripts -- Example: rc = seqstore({"abc","def"}, {2,1}, 'q') -- --> rc is {"abc","qef"} -- Example: rc = seqstore({"abc","def"}, {1,2}, 'q') -- --> rc is {"aqc","def"} ---------------------------------------- global function seqstore(object piSource, object piIndex, object piValue) ---------------------------------------- -- store piValue in piSource using index sequence piIndex integer lLen if atom(piSource) then return piValue end if if atom(piIndex) then piSource[piIndex] = piValue else lLen = length(piIndex) if lLen > 1 then piSource[piIndex[1]] = seqstore(piSource[piIndex[1]], piIndex[2..lLen], piValue) else piSource[piIndex[1]] = piValue end if end if return piSource end function > 2: Not much difference in coding terms: > > -- Get 's' from somewhere, then... > params = {a, b, c} > t = repeat(0, length(params)) > for i = 1 to length(params) do > t[i] = s[params[i]] > end for > -- t is now {s[a],s[b],s[c]} Again my code is very similar, just more validation checking and an 'exclude/include' tweak... ---------------------------------------- global function subset(sequence piSource, sequence piIndex, integer pFlag) ---------------------------------------- -- Pickout from a sequence, all the elements identified in the Index sequence -- If Flag = true then include all the elements, otherwise exclude them. sequence lResult integer lSub integer lUsed -- If excluding elements, rejig the index sequence. if pFlag = 0 then -- Init to a null set lResult = {} -- Examine each possible element subscript for i = 1 to length(piSource) do -- If subscript not in original list, then include it. if find(i, piIndex) = 0 then lResult &= i end if end for -- Replace original list with new list. piIndex = lResult end if lResult = repeat(0, length(piIndex)) lUsed = 0 -- for each subscript in the index sequence: for i = 1 to length(piIndex) do lSub = piIndex[i] -- if the subscript in valid for the source seq, if lSub >= 1 and lSub <= length(piSource) then -- collect the element lUsed += 1 lResult[lUsed] = piSource[lSub] end if end for return lResult[1 .. lUsed] end function > 3: Pretty much speaks for itself :) Hmmmm.... My associative array library is way to big to copy into this reply. -- Derek