list comprehensions: sorting?
Clark C . Evans
cce at clarkevans.com
Fri Sep 7 08:59:01 EDT 2001
On Fri, Sep 07, 2001 at 11:29:12AM +0200, Alex Martelli wrote:
| And if you can think of some 'orderby' syntax that
| would actually cover ALL typical sorting needs with
| just "a bit" of extension to the current one, we could
| surely write a PEP for it -- but it would HAVE to be
| "just a bit" (of extra syntax), and for very wide generality
| gains, or the trade-off is a losing one, I fear.
Consider the following pattern...
[ vSelect for vAs in vFrom if vWhere sortby vOrder ]
This could be expanded into a farily generic processing
template where vOrder is any expression..
sortby = 1
temp = []
for vAs in vFrom:
if vWhere:
val = process(x)
if sortby: val = (vOrder,val)
temp.append(val)
if sortby:
temp.sort()
if is_desc: temp.reverse()
sorted = []
for x in temp: sorted.append(x[1])
temp = sorted
return temp
The primary problem is that sort has a few "arguments",
as to sort descending/ascending or to do a stable sort;
how about taking a function like syntax? sortby(desc,stable)
Thoughts?
Clark
P.S. I also had a very fuzzy idea in my head about
a syntax extension registry function. This may
be way too powerful; but it appears all of the
list extensions could be expressed as re-writes
of the parse tree (before compile). So, we could
add a function like...
register regex, rewrite_function
In this way "statements" which were not recognized
by the parser would be passed through the regex of
each registered rewrite rule. If the rewrite matched,
then the rewrite_function would be called on the
statement at compile time to produce a representation
that is in the core language.
Pretty powerful... probably too powerful.
More information about the Python-list
mailing list