[Python-Dev] Re: "groupby" iterator
Samuele Pedroni
pedronis at bluewin.ch
Thu Dec 4 08:09:43 EST 2003
At 16:22 04.12.2003 +1300, Greg Ewing wrote:
>Greg Ball <gball at cfa.harvard.edu>:
>
> > (x[1] for x) == lambda x:x[1]
> > (x.score for x) == lambda x: x.score
> > (x+y for x,y) == lambda x,y: x+y
> > (len(x) for x) == lambda x,len=len: len(x) # roughly equivalent
>
>Interesting idea, but it reads a bit strangely.
>
>Maybe this would work better with a different keyword...
>
> x[1] given x
> x.score given x
> x+y given x,y
> len(x) given x
>
> stuff = groupby((x for x in seq), key = (x.score given x))
on the plus side it nicely parallels generator expressions and the keyword
makes lookup in the docs easy. On the minus side is maybe verbose.
On the other hand non-keyword notations risk to be too cryptic: some
candidates:
stuff = groupby((x for x in seq), key = (x.score\x))
# this would require the lexer to distinguish \ as line continuation vs
this usage,
# it would probably still be read as "x.score given x"
stuff = groupby((x for x in seq), key = (x-->x.score)) # --> would be a
single token
More information about the Python-Dev
mailing list