File tree Expand file tree Collapse file tree 1 file changed +16
-6
lines changed Expand file tree Collapse file tree 1 file changed +16
-6
lines changed Original file line number Diff line number Diff line change @@ -720,12 +720,22 @@ def partition_all(n, seq):
720720 yield prev
721721 prev = item
722722 if prev [- 1 ] is no_pad :
723- # Get first index of no_pad without using .index()
724- # https://github.com/pytoolz/toolz/issues/387
725- for ind , item in enumerate (reversed (prev )):
726- if item is not no_pad :
727- yield prev [:len (prev )- ind ]
728- break
723+ try :
724+ # If seq defines __len__, then we can quickly calculate where no_pad starts
725+ yield prev [:len (seq ) % n ]
726+ except TypeError :
727+ # Get first index of no_pad without using .index()
728+ # https://github.com/pytoolz/toolz/issues/387
729+ # We can employ modified binary search here to speed things up from O(n) to O(log n)
730+ # Binary search from CPython's bisect module, modified for identity testing.
731+ lo , hi = 0 , len (prev )
732+ while lo < hi :
733+ mid = (lo + hi ) // 2
734+ if prev [mid ] is no_pad :
735+ hi = mid
736+ else :
737+ lo = mid + 1
738+ yield prev [:lo ]
729739 else :
730740 yield prev
731741
You can’t perform that action at this time.
0 commit comments