Skip to content

Commit

Permalink
@ sums left-to-right for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
HighDiceRoller committed Aug 30, 2024
1 parent 082828a commit 40e9551
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/icepool/population/die.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,9 @@ def _sum_cache(self) -> MutableMapping[int, 'Die']:
def _sum_all(self, rolls: int, /) -> 'Die':
"""Roll this `Die` `rolls` times and sum the results.
The sum is computed one at a time, with the new item on the right,
similar to `functools.reduce()`.
If `rolls` is negative, roll the `Die` `abs(rolls)` times and negate
the result.
Expand All @@ -750,8 +753,9 @@ def _sum_all(self, rolls: int, /) -> 'Die':
elif rolls == 1:
result = self
else:
# Binary split seems to perform much worse.
result = self + self._sum_all(rolls - 1)
# In addition to working similar to reduce(), this seems to perform
# better than binary split.
result = self._sum_all(rolls - 1) + self

self._sum_cache[rolls] = result
return result
Expand Down

0 comments on commit 40e9551

Please sign in to comment.