On Mon, 1 Aug 2022 at 13:50, <arvindsis...@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)
SymPy has a symbolic Range object that interacts with its other Set types: In [9]: from sympy import * In [10]: Range(10) - Range(5) Out[10]: {0, 1, …, 9} \ {0, 1, …, 4} In [11]: list(_) Out[11]: [5, 6, 7, 8, 9] In [12]: Range(3, 5, 2) + Range(3, 6, 2) Out[12]: {3} ∪ {3, 5} That last example could be simplified but I guess the simplification code for it has not been added. https://docs.sympy.org/latest/modules/sets.html#range The Range type is a bit weird because other SymPy Sets are not considered to be ordered but Range is. Some operations will not preserve the ordering because it isn't really well defined in general set arithmetic: In [35]: Range(10) Out[35]: {0, 1, …, 9} In [36]: Range(10)[::-1] Out[36]: {9, 8, …, 0} In [37]: Range(10) & Range(10)[::-1] Out[37]: {0, 1, …, 9} In [38]: Range(10)[::-1] & Range(10) Out[38]: {0, 1, …, 9} > I believe this could be a fair first step into allowing for a whole range of > mathematical operations in abstract algebra. Take a look at SymPy's sets module. There are various other kinds of sets and things such as: In [26]: Range(2) * Range(3) Out[26]: {0, 1} × {0, 1, 2} In [27]: list(_) Out[27]: [(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (1, 2)] In [28]: Range(10) - {3} Out[28]: {0, 1, …, 9} \ {3} I don't think it makes sense to make any changes to the builtin range objects for these kinds of operations. -- Oscar _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/NTFZ7NTRP7MCPMWZ54EIV6CFGHKYEXUJ/ Code of Conduct: http://python.org/psf/codeofconduct/