You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"""Returns an iterator of paired items, overlapping, from the original
>>> take(4, pairwise(count()))
[(0, 1), (1, 2), (2, 3), (3, 4)]
On Python 3.10 and above, this is an alias for :func:`itertools.pairwise`.
"""
a, b=tee(iterable)
next(b, None)
yieldfromzip(a, b)
try:
fromitertoolsimportpairwiseasitertools_pairwise
exceptImportError:
pairwise=_pairwise
else:
defpairwise(iterable):
yieldfromitertools_pairwise(iterable)
pairwise.__doc__=_pairwise.__doc__
In both cases, yield from can be replaced with return.
I saw the Grudgingly satisfy mypy commit that made them generators and I can reproduce a mypy error with that old code using functools.partial, but with my above return suggestion, mypy has no issue.
As-is, it doesn't run in pre-3.10, since it uses itertools.pairwise. Here's a version I ran with 3.8.12 (maybe on a different machine, not comparable with the above results):
22.7 ± 0.4 ms proposal
47.4 ± 1.3 ms more_itertools
Or rather "Don't artificially slow it down with a generator". Times for pairwise on a million elements (with Python 3.10.8):
The current code:
more-itertools/more_itertools/recipes.py
Lines 294 to 317 in 3ac895f
In both cases,
yield from
can be replaced withreturn
.I saw the Grudgingly satisfy mypy commit that made them generators and I can reproduce a mypy error with that old code using
functools.partial
, but with my abovereturn
suggestion, mypy has no issue.Benchmark script
The text was updated successfully, but these errors were encountered: