Skip to content

Commit

Permalink
Merge branch 'eltoder-feature/numeric-range-calc-len' into issue-732-…
Browse files Browse the repository at this point in the history
…isqrt
  • Loading branch information
bbayles committed Jul 24, 2023
2 parents 9514d07 + a67c030 commit 8ed237d
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions more_itertools/more.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections import Counter, defaultdict, deque, abc
from collections.abc import Sequence
from functools import partial, reduce, wraps
from functools import cached_property, partial, reduce, wraps
from heapq import heapify, heapreplace, heappop
from itertools import (
chain,
Expand Down Expand Up @@ -2075,7 +2075,6 @@ def __init__(self, *args):
if self._step == self._zero:
raise ValueError('numeric_range() arg 3 must not be zero')
self._growing = self._step > self._zero
self._init_len()

def __bool__(self):
if self._growing:
Expand Down Expand Up @@ -2151,7 +2150,8 @@ def __iter__(self):
def __len__(self):
return self._len

def _init_len(self):
@cached_property
def _len(self):
if self._growing:
start = self._start
stop = self._stop
Expand All @@ -2162,10 +2162,10 @@ def _init_len(self):
step = -self._step
distance = stop - start
if distance <= self._zero:
self._len = 0
return 0
else: # distance > 0 and step > 0: regular euclidean division
q, r = divmod(distance, step)
self._len = int(q) + int(r != self._zero)
return int(q) + int(r != self._zero)

def __reduce__(self):
return numeric_range, (self._start, self._stop, self._step)
Expand Down

0 comments on commit 8ed237d

Please sign in to comment.