<div dir="ltr">Changing the builtin map to do this would be questionable because there is some performance overhead to dispatching to the inner coroutine's `send` methods. `__next__` is implemented as a tp slot; however, coroutine `send` is just a normal method that needs to be looked up every time we call next. While my implementation is not as optimized as it could be, it is about twice as slow as builtin map (and 4x faster is . I think it would be better to keep the library separate until there is a lot of demand for this feature.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 12, 2016 at 6:02 PM, Bar Harel <span dir="ltr"><<a href="mailto:bzvi7919@gmail.com" target="_blank">bzvi7919@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Perhaps something like this should go in the standard library? Or change the builtin map to forward everything? Changing the builtin map in this case will be backwards compatible and will overall be beneficial. If someone made a workaround, the workaround would still work, but now the builtins will do it per se.</div><div class="HOEnZb"><div class="h5"><br><div class="gmail_quote"><div dir="ltr">On Wed, Apr 13, 2016 at 12:55 AM Joseph Jevnik <<a href="mailto:joejev@gmail.com" target="_blank">joejev@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div>I have written a library `cotoolz` that provides the primitive pieces needed for this. It implements a `comap` type which is like `map` but properly forwards `send`, `throw`, and `close` to the underlying coroutine. There is no special syntax needed, just write:<br><br>```<br></div>yield from comap(f, inner_coroutine())<br>```<br><br></div>This also supports `cozip` which lets you zip together multiple coroutines, for example:<br><br>```<br></div>yield from cozip(inner_coroutine_a(), inner_corouting_b(), inner_coroutine_c(), ...)<br>```<br><br></div>This will fan out the sends to all of the coroutines and collect the results into a single tuple to yield. Just like `zip`, this will be exchausted when the first coroutine is exhausted.<br><div><div><div><div><div><br>The library is available as free software on pypi or on here: <a href="https://github.com/llllllllll/cotoolz" target="_blank">https://github.com/llllllllll/cotoolz</a><br></div></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"></div></div><div class="gmail_extra"><div class="gmail_quote">On Tue, Apr 12, 2016 at 5:40 PM, Bar Harel <span dir="ltr"><<a href="mailto:bzvi7919@gmail.com" target="_blank">bzvi7919@gmail.com</a>></span> wrote:<br></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I asked a question in <a href="http://stackoverflow.com/q/36582679/1658617" target="_blank">stackoverflow</a> regarding a way of modifying yield from's return value.<div>There doesn't seem to be any way of modifying the data yielded by the yield from expression without breaking the yield from "pipe". By "pipe" I mean the fact that .send() and .throw() pass to the inner generator. Useful cases are parsers as I've demonstrated in the question, transcoders, encoding and decoding the yielded values without interrupting .send() or .throw() and generally coroutines.</div><div><br></div><div>I believe it would be beneficial to many aspects and libraries in Python, most notably asyncio.</div><div><br></div><div>I couldn't think of a good syntax though, other than creating a wrapping function, lets say in itertools, that creates a class overriding send, throw and __next__ and receives the generator and associated modification functions (like suggested in one of the answers).</div><div><br></div><div>What do you think? Is it useful? Any suggested syntax?</div></div>
<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div><br></div>
</blockquote></div>
</div></div></blockquote></div><br></div>