On 2020-10-25 at 16:34:14 +0000,
George Harding <[email protected]> wrote:
> some_iter = map(lambda x: x if print(x) else x, some_iter)
>
> The tuple has a ~50% overhead, the case statement ~15%, compared to the
> generator.
def print_first(x):
print(x)
return x
new_iter = map(print_first, some_iter)
No extranous tuple, no extranous case stament. Newlines are cheap these
days. The call to print, however, is possibly unbounded in time and
space.
If you really want to go overboard, put something like the following
peeker function into your personal toolbox (untested):
def peeker(function):
def peeker(x):
function(x)
return x
return peeker
And use it like this:
new_iter = map(peeker(print), some_iter)
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/RG2ZQD5NNU2BKVTC2AZGSQNVZA2CV2UJ/
Code of Conduct: http://python.org/psf/codeofconduct/