
On Mon, 1 Aug 2022 at 13:50, <arvindsistla@gmail.com> wrote:
Hello everyone. First time here, so please be forgiving of any missteps on my part. This is an idea I have thought about for a while, and I believe it could be quite useful. Would it be possible to enhance the standard range() type to allow for elementary operations on ranges? For example : range(10) - range(5) => range(5, 10) range(3, 5, 2) + range(3, 6, 2) => range(3, 6, 2)
There are a lot of complex cases you'd need to consider. What would the value of range(3, 15, 2) + range(8, 12, 2) be? It's not a range in the sense of being describable as (start, end, step). And simply saying "that's not allowed" wouldn't really work, as it would be far too hard to work with if operations could fail unexpectedly like this. In reality, this feels more like you're after set algebra, which Python already has. I believe this could be a fair first step into allowing for a whole range
of mathematical operations in abstract algebra.
For cases where the number of elements in the range is not ridiculously large, simply converting to sets is probably sufficient:
set(range(10)) - set(range(5)) == set(range(5, 10)) True set(range(3,5,2)) | set(range(3,6,2)) == set(range(3,6,2)) True
For sufficiently large ranges, there are bitset classes on PyPI that might be more memory efficient. Or if you're looking for some other types of operation (unbounded ranges, or specialised abstract algebra operations) a custom class (maybe even published on PyPI) would probably be a better approach than trying to get something that specialised added to the stdlib. Paul