Built-in func to calculate negative indices?? 
Author Message
 Built-in func to calculate negative indices??

Do we want a built-in function to calculate the absolute (ie, positive)
values of negative subscript indices? For example:-

x = "python"

x[-2]        # = "o"; positive index = 4

pos = absIndex(len(x), -2)    # pos = 4

x[pos]        # = "o"

The "absIndex" function would have the semantics of the following python
function:-

    def absIndex(index, size):
        if index >= 0:
            result = index
        elif index < 0:
            result = index + size

        if (result < 0) or (result >= size):
            return None        # ie, invalid result
        else:
            return result

It would be possible to constrain the return value to something between 0
and size - 1 within the function (using min and max), but this would make it
hard to spot index errors. You would, however, want to constrain the return
value when dealing with slices, so perhaps a separate "absSlice" would be
useful as well.

Of course it is very easy to implement these functions yourself in Python,
but I bet this same code is written over and over again by people
re-defining the subscript operators. Built-in functions might make the code
a bit more standard and self-documenting.

&.



Sun, 05 Oct 2003 02:49:42 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. How to calculate negative binary??

2. negative array index

3. Negative array indices

4. question about negative indices in fortran 77

5. question about negative indices in fortran 77

6. Q : legacy of negative index

7. getting number of arguments of builtin/C func

8. Passing variables from Func to Func

9. QUESTION: Passing variables from Func to Func???

10. i want to build a comparator to compare voltages such that reference voltage is negative

11. Index Build

12. Build Index

 

 
Powered by phpBB® Forum Software