"pre-"*10 + "PEP": extension to the list comprehensions & genexps syntax

Gregory Petrosyan gregory.petrosyan at gmail.com
Wed Jan 25 14:07:14 EST 2006


This is revised version of "Arithmetic sequences in Python" proposal.

I don't want this topic to become as bloated with offtopic as "ASiP".
Please post here corresponding info only. Your (revised) comments
(possibly already posted to "ASiP") are welcome.

The first thing I want to mention is that, as far as I have
discovered, there are currently 3 PEPs built upon *similar* (but not
the same) idea. These are: PEP 204 ("Range Literals", rejected), PEP
276 ("Simple Iterator for ints", rejected) and PEP 284 ("Integer
for-loops", rejected). You are welcome to look at them all. (Take a
look at "References" section for details).

Another interesting discovery is that Jim Althoff in PEP 276 had
mentioned Haskell's notation: "  --  adding Haskell-like notation for
specifying a sequence of integers in a literal list".


Now, here is the revised proposal:
"Extend current list comprehensions and generator expressions syntax
with Haskell-style notation".
(Note the focus is  _not_  on the iteration itself, but on extended
comps/genexps).
The main idea of the proposal is that current
index/slicing/range-based syntax is often not obvious and easy to
read, and that it can be extended to be better. Compare:

[20, 25 .. 100]  vs.  list(range(20, 101, 5))
[55 .. 73]  or  list(55 .. 73)  vs.  list(range(55, 74))
[1, 3 .. len(mystr)]  vs.  list(range(1, len(mystr)+1, 2))
[55, 65 .. 295]  vs.  list(range(55, 296, 10))   and

def arithmetic_sequence(start, step=1):
    yield start
    while 1:
        start += step
        yield start

vs.  (first, next ..)     :-)


More or less detailed description (at first, for integers):
1) List comprehensions.
I propose two new forms of syntax. Let the first one be the "full
form": [first, next .. last] (first, next and last are integers):
[1, 3 .. 10]   ->  [1, 3, 5, 7, 9]
[0, -2 .. -8]  ->  [0, -2, -4, -6, -8]
Here: [0, -2 .. -8] is [0, -2, -4, -6, -8, -10, -12 ......] limited to x >= -8.
The second form is "short form": [first .. last]:
[10 .. 15]  ->  [10, 11, 12, 13, 14, 15]
So, if "next" is omitted, default is "first+1"

2) Generator expressions.
Besides these two forms, I propose 2 additional forms for creating
'infinite' generators:
(-10, -20 ..)  ->  yields -10, -20, -30, .... and so on
(48 ..)  ->  yields 48, 49, 50 and so on
So, if "last" is omitted, default is inf.


Maybe:
- add support for floating point numbers, like
[5, 6.5 .. 10]  ->  [5.0, 6.5, 8.0, 9.5]
- for "short form": add support for chars, like
('a' .. 'z')  ->  yields English alphabet
('a' ..)  ->  yields chars from chartable (ASCII?) from 'a' to the end?


Open issues:
- ".." vs "...". The first is short, but probably the second is more consistent
- [f(n), f(n)-1 .. 0]: f(n) would be calculated twice (and the results
may differ!). It can be solved by using something like "where" keyword
(or maybe "with"?).
- what about [-5 .. -10]? Should expressions like this produce
SomeError, or pass silently? Or maybe there's some different solution?


References:
-  "ASiP" (IMO first 10 (20?) posts are most meaningful):
http://groups.google.com/group/comp.lang.python/browse_frm/thread/5259638f3703330f?hl=en&
-  Very interesting competing proposal there:
http://groups.google.com/group/comp.lang.python/msg/cf117325862bbd7a
-  PEP 204: http://www.python.org/peps/pep-0204.html
-  PEP 276: http://www.python.org/peps/pep-0276.html
-  PEP 284: http://www.python.org/peps/pep-0284.html

--
Regards, Gregory.


More information about the Python-list mailing list