[Tcl9-cloverfield] Vector index references
Status: Alpha
Brought to you by:
fbonnet
From: Andy G. <unu...@ai...> - 2009-01-17 08:08:13
|
Today I spent some time contemplating vector index references and how they can be used. In my language (to date), there are really two kinds of vector index references: references to elements and references to sublists ("slices", in Python parlance). An individual index has one of the following forms: 1. expression ([expr]-style, must evaluate to an integer) 2. end 3. end-expression 4. end+expression Examples: "5", "$a", "$a*$b", "end", "end-$a*$b", "end - $a * $b", "end-1" Case (3) above is equivalent to case (4) with a "-" prepended to the expression, so "end-$a+$b" means "end+(-$a)+$b" and not "end-($a+$b)". In case (2), (3), and (4), "end" is numerically equal to the list length minus one. A vector index has one of the following forms, where index and index2 are defined according to the above table: 1. {index} - refers to the single element at index - if end, refers to the last element - if negative, refers to the nonexistent element before the first element - if equal to or greater than the list length, refers to the nonexistent element after the last element 2. {index:} - refers to the empty list "between" index and its predecessor - if end, refers to the space before the last element - if zero or negative, refers to the space before the first element - if equal to or greater than the list length, refers to the space after the last element 3. {index:index2} - refers to the list of all elements starting at index and ending at index2, inclusive - equivalent to form (4) with +1 stride - if index2 is less than index, then equivalent to form (2) 4. {index:index2:stride} - refers to the list of all elements starting at index and ending at index2 - only every stride'th element is included - the index element is always included - the index2 element may not be included, depending on stride - stride is an [expr]-style expression - stride must be a positive or negative integer, but not zero - if index is equal to index2, refers to the single-element list containing index, regardless of stride - if the sign of stride is not equal to the sign of index2-index, equivalent to form (2) Examples of {index}: set &var (a b c) -> a b c d set &var{0} A -> A b c d set &var{end} D -> A b c D set &var{end+1} -> error: doesn't exist set &var{end+1} E -> A b c D E set &var{end+9} F -> A b c D E F set &var{-1} -> error: doesn't exist set &var{-1} Z -> Z A b c D E F set &var{-9} Y -> Y Z A b c D E F Examples of {index:}: set &var{0:} (W X) -> W X Y Z A b c D E F set &var(-9:} (V) -> V W X Y Z A b c D E F set &var{1:} (0 1) -> V 0 1 W X Y Z A b c D E F set &var{end:} (2 3) -> V 0 1 W X Y Z A b c D E 2 3 F set &var{end+1:} (G H) -> V 0 1 W X Y Z A b c D E 2 3 F G H set &var{end+9:} (I) -> V 0 1 W X Y Z A b c D E 2 3 F G H I It's well past my bedtime... I will write more tomorrow! -- Andy Goth | http://andy.junkdrome.org/ unununium@{aircanopy.net,openverse.com} |