[Python-ideas] + operator on generators
Koos Zevenhoven
k7hoven at gmail.com
Mon Jun 26 16:26:48 EDT 2017
On Mon, Jun 26, 2017 at 4:53 PM, Serhiy Storchaka <storchaka at gmail.com>
wrote:
> 26.06.17 13:47, Joao S. O. Bueno пише:
>
>> On 25 June 2017 at 20:55, Danilo J. S. Bellini <danilo.bellini at gmail.com
>> <mailto:danilo.bellini at gmail.com>> wrote:
>>
>> On Sun, Jun 25, 2017 at 3:06 PM, lucas via Python-ideas
>> <python-ideas at python.org
>> <mailto:python-ideas at python.org>>wrote:
>>
>> I often use generators, and itertools.chain on them.
>> What about providing something like the following:
>>
>> a = (n for n in range(2))
>> b = (n for n in range(2, 4))
>> tuple(a + b) # -> 0 1 2 3
>>
>>
>> You know you can do `tuple(*a, *b)` , right?
>>
>> The problem with the "*" notation is that it will actually render the
>> iterable
>> contents eagerly - unlike something that would just chain them.
>> But for creating tuples, it just works.
>>
>
> Even the tuple constructor is not needed.
>
> >>> *a, *b
> (0, 1, 2, 3)
>
And you can also do
def a_and_b():
yield from a
yield from b
c = a_and_b() # iterable that yields 0, 1, 2, 3
I sometimes wish there was something like
c from:
yield from a
yield from b
...or to get a list:
c as list from:
yield from a
yield from b
...or a sum:
c as sum from:
yield from a
yield from b
These would be great for avoiding crazy oneliner generator expressions.
They would also be equivalent to things like:
@list
@from
def c():
yield from a
yield from b
@sum
@from
def c():
yield from a
yield from b
the above, given:
def from(genfunc):
return genfunc()
Except of course `from` is a keyword and it should probably just be `call`.
But this still doesn't naturally extend to allow indexing and slicing, like
c[2] and c[1:3], for the case where the concatenated iterables are
Sequences.
-- Koos
--
+ Koos Zevenhoven + http://twitter.com/k7hoven +
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170626/4d78ca12/attachment.html>
More information about the Python-ideas
mailing list