Skip to content

Commit

Permalink
deprecate depth=None in favor of depth='inf'
Browse files Browse the repository at this point in the history
  • Loading branch information
HighDiceRoller committed Nov 23, 2024
1 parent 93fbf22 commit f96cea2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/icepool/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def map(
of the process up to this point. If you only want to reroll the
current stage, you can nest another `map` inside `repl`.
EXPERIMENTAL: If set to `None`, the result will be as if this
EXPERIMENTAL: If set to `'inf'`, the result will be as if this
were repeated an infinite number of times. In this case, the
result will be in simplest form.
time_limit: Similar to `repeat`, but will return early if a fixed point
Expand Down
18 changes: 12 additions & 6 deletions src/icepool/population/die.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import itertools
import math
import operator
import warnings

from typing import Any, Callable, Collection, Container, Iterable, Iterator, Literal, Mapping, MutableMapping, Sequence, Set, cast, overload

Expand Down Expand Up @@ -304,7 +305,7 @@ def reroll(self,
/,
*,
star: bool | None = None,
depth: int | None) -> 'Die[T_co]':
depth: int | Literal['inf']) -> 'Die[T_co]':
"""Rerolls the given outcomes.
Args:
Expand All @@ -330,7 +331,12 @@ def reroll(self,
else:
outcome_set = self._select_outcomes(which, star)

if depth is None:
if depth == 'inf' or depth is None:
if depth is None:
warnings.warn(
"depth=None is deprecated; use depth='inf' instead.",
category=DeprecationWarning,
stacklevel=1)
data = {
outcome: quantity
for outcome, quantity in self.items()
Expand Down Expand Up @@ -359,7 +365,7 @@ def filter(self,
/,
*,
star: bool | None = None,
depth: int | None) -> 'Die[T_co]':
depth: int | Literal['inf']) -> 'Die[T_co]':
"""Rerolls until getting one of the given outcomes.
Essentially the complement of `reroll()`.
Expand Down Expand Up @@ -624,9 +630,9 @@ def mean_time_to_sum(self: 'Die[int]', target: int, /) -> Fraction:
self.denominator() - self.quantity(0))

for i in range(len(self._mean_time_to_sum_cache), target + 1):
result = time_per_effect + self.reroll(
[0],
depth=None).map(lambda x: self.mean_time_to_sum(i - x)).mean()
result = time_per_effect + self.reroll([
0
], depth='inf').map(lambda x: self.mean_time_to_sum(i - x)).mean()
self._mean_time_to_sum_cache.append(result)

return result
Expand Down
4 changes: 2 additions & 2 deletions tests/again_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_is_additive():
def test_again_count(n):
count = Die([1, 2, 3, 4, 5, 6 + Again], again_count=n)
depth = d6.explode(depth=n)
depth = depth.reroll([depth.max_outcome()], depth=None)
depth = depth.reroll([depth.max_outcome()], depth='inf')
assert count == depth


Expand All @@ -115,5 +115,5 @@ def test_again_count_double_blocked():
def test_again_count_double():
result = Die([1, 2, 3, 4, 5, 6 + Again + Again], again_count=2)
bonus = 2 @ d6.map({6: 100})
expected = d6.map({6: 6 + bonus}).reroll(lambda x: x > 100, depth=None)
expected = d6.map({6: 6 + bonus}).reroll(lambda x: x > 100, depth='inf')
assert result == expected
12 changes: 6 additions & 6 deletions tests/reroll_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@


def test_reroll_default():
result = icepool.d6.reroll(depth=None)
result = icepool.d6.reroll(depth='inf')
expected = icepool.d5 + 1
assert result.equals(expected)


def test_reroll_1():
result = icepool.d6.reroll([1], depth=None)
result = icepool.d6.reroll([1], depth='inf')
expected = icepool.d5 + 1
assert result.equals(expected)


def test_reroll_2():
result = icepool.d6.reroll([1, 2], depth=None)
result = icepool.d6.reroll([1, 2], depth='inf')
expected = icepool.d4 + 2
assert result.equals(expected)

Expand Down Expand Up @@ -62,18 +62,18 @@ def test_reroll_depth_3_mixed():


def test_infinite_reroll():
assert icepool.d4.reroll([1, 2, 3, 4], depth=None).is_empty()
assert icepool.d4.reroll([1, 2, 3, 4], depth='inf').is_empty()


def test_reroll_multidim():
result = icepool.Die([(1, 0), (0, 1)]).reroll(lambda x: x[0] == 0,
depth=None)
depth='inf')
expected = icepool.Die([(1, 0)])
assert result.equals(expected)


def test_reroll_until_multidim():
result = icepool.Die([(1, 0), (0, 1)]).filter(lambda x: x[0] == 0,
depth=None)
depth='inf')
expected = icepool.Die([(0, 1)])
assert result.equals(expected)
8 changes: 4 additions & 4 deletions tests/vector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ def test_map_star():

def test_reroll_star():
result = icepool.vectorize(d6, d6)
result = result.reroll(lambda a, b: a == 6 and b == 6, depth=None)
result = result.reroll(lambda a, b: a == 6 and b == 6, depth='inf')
result = result.map(lambda a, b: a + b)
expected = (2 @ icepool.d6).reroll({12}, depth=None)
expected = (2 @ icepool.d6).reroll({12}, depth='inf')
assert result.equals(expected)


def test_filter_star():
result = icepool.vectorize(d6, d6)
result = result.filter(lambda a, b: a == 6 and b == 6, depth=None)
result = result.filter(lambda a, b: a == 6 and b == 6, depth='inf')
result = result.map(lambda a, b: a + b)
expected = (2 @ icepool.d6).filter({12}, depth=None)
expected = (2 @ icepool.d6).filter({12}, depth='inf')
assert result.equals(expected)


Expand Down

0 comments on commit f96cea2

Please sign in to comment.