On Fri, Jan 08, 2021 at 11:19:40PM +0200, Ram Rachum wrote:
> Today I had a need: I had a tuple of dynamic sequence-like objects. I
> wanted to iterate on them reversed, starting with items of the last one and
> slowly making my way towards the first one.
> 
> In short, I want `reversed(itertools.chain(x, y, z))` that behaves like
> `itertools.chain(map(reversed, (z, y, x)))`.

That would break backwards compatibility, because 
`reversed(itertools.chain(x, y, z))` is already possible today, and it 
does *not* behave in that fashion.

reversed reverses whatever iterable it is given, it doesn't single out 
chain objects for special magical handling. If I write this:

    reversed([None, 'abc', 'def', 'ghi'])

I expect to get

    'ghi', 'def', 'abc', None

and not

    'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', raise TypeError

If I replace the list with itertools.chain, I should still get exactly 
the same results, and I do. Breaking backwards compatibility for your 
special case is not going to happen.

> What do you think?

I think you should just write `itertools.chain(map(reversed, (z, y, x)))`.

If you don't have the individual x, y, z sequences, but only their 
tuple t = (x, y, z), you have two choices:

    itertools.chain(map(reversed, t[::-1]))

    itertools.chain(map(reversed, reversed(t)))

They have slightly different meanings, so you get to choose whichever 
one suits your use-case better.

Not every trivial combination of functions needs to be given a built-in 
or standard library solution. Especially not if doing so will break 
backwards compatibility.

"I had three numbers in a tuple, and wanted half of twice the first 
number added to the difference of the remaining two. What do you think 
about making `len((a,b,c))` return `(2*a + abs(b - c))/2`?"

*wink*


-- 
Steve
_______________________________________________
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/PEXRRXQGJV3YQIFTKJKGQRF7FQAKTVY7/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to