Re: Last Element Reference
- Posted by Robert Craig <rds at RapidEuphoria.com> Sep 19, 2003
- 478 views
Derek Parnell wrote: > If I may, I'd like to supply a requirements document for the proposed > enhancement to Euphoria. > > Title: Last Element Reference > > Functional Requirement: > To enable a coder to reference the last element in a sequence, within > slice specifications and element references, without having to > explicitly name the sequence itself. > > Benefits: > * Reduce the amount of typing. > * Reduce the possibility of misspelling a identifier. > * Improve legibility by reducing the number of characters to read. > * Improve interpreter performance by having less tokens to process. > > Costs: > * If using a new syntax symbol, it will reduce the number of available > symbols for future use. > > Technical Specification: > I suggest that the character '$' be used as a shorthand for > "length(<seq>)" when '$' appears inside a slice specification or inside > an element reference. And that the sequence it refers to is the same one > that the slice and element reference is using. > > Examples: > > sequence X > > X = {"ant", "bear", "cat", "dog", "elephant"} > > ? X[$] --> "elephant" > ? X[$][$] --> 't' > ? X[3 .. $] --> {"cat", "dog", "elephant"} > ? X[3 .. $-1] --> {"cat", "dog"} > ? X[$-1..$] --> {"dog", "elephant"} > ? X[$][3..$] --> "ephant" > ? X[$][$-3..$] --> "hant" > ? X[2][$] --> 'r' > ? X[1 .. $-1] --> {"ant", "bear", "cat", "dog"} > ? X[1 .. floor($/2)] --> {"ant", "bear"} > ? X[floor($/2)+1 .. $] --> {"cat", "dog", "elephant"} Yes. That's exactly what I was thinking. There are some weird cases too, like: x[y[$]..$] -- $ refers to the immediately enclosing brackets x[$ * $ - foo($)] -- $ could be used anywhere, in any way -- inside the brackets $ would be a kind of very localized constant with a scope from [ to ]. It's value would be the length of the sequence being subscripted or sliced. Pete Lomax wrote: > Since you asked, I'm [still] sold on negative indexes: > > s="(0,0)" > x=s[1..-1] -- x is now "(0,0)" > x=s[2..-2] -- x is now "0,0" > x=s[3..-3] -- x is now "," > > s="abc.exw" > if equal(s[1..3],"abc") then -- this is true > if equal(s[-3..-1],"exw") then -- this is true > > s=s[3..-1] -- removes the first two elements of s > s=s[1..-3] -- removes the last two elements of s > > s=s[1..2] -- selects the first two elements of s > s=s[-2..-1] -- selects the last two elements of s I find negative subscripts to be a bit less clear. Plus I'd like to continue to be able to catch all negative numbers as bad subscripts. > "end" may be a more readable alternative to $. > When "end" is between [] should be no major parsing problem? "end" could be parsed easily enough using the RDS parser, but I think it would be visually confusing for a person reading the code, especially a beginner. He might confuse it with "end if", "end for", "end procedure" etc., or he might think "end" was a variable, and might wonder where it was assigned or declared. Regards, Rob Craig Rapid Deployment Software http://www.RapidEuphoria.com