Skip to content

Commit

Permalink
die.map_to_pool with no arguments now maps the outcomes directly
Browse files Browse the repository at this point in the history
add some tests
  • Loading branch information
HighDiceRoller committed Dec 29, 2023
1 parent ee03c5b commit 99dbf38
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/icepool/population/die.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,15 +907,22 @@ def middle(
def map_to_pool(
self,
repl:
'Callable[..., Sequence[icepool.Die[U] | U] | Mapping[icepool.Die[U], int] | Mapping[U, int] | icepool.Reroll]',
'Callable[..., Sequence[icepool.Die[U] | U] | Mapping[icepool.Die[U], int] | Mapping[U, int] | icepool.Reroll] | None' = None,
/,
*extra_args: 'Outcome | icepool.Die | icepool.MultisetExpression',
star: bool | None = None
) -> 'icepool.MultisetGenerator[U, tuple[int]]':
"""EXPERIMENTAL: Maps outcomes of this `Die` to `Pools`, creating a `MultisetGenerator`.
As `icepool.map_to_pool(repl, self, ...)`.
If no argument is provided, the outcomes will be used to construct a
mixture of pools directly, similar to the inverse of `pool.expand()`.
Note that both are typically not particularly efficient since they do
not make much use of dynamic programming.
"""
if repl is None:
repl = lambda x: x
return icepool.map_to_pool(repl, self, *extra_args, star=star)

def explode_to_pool(
Expand Down
12 changes: 12 additions & 0 deletions tests/map_to_pool_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import icepool
import pytest

from icepool import d6


def test_explode_to_pool():
assert d6.explode_to_pool(3, depth=4).sum() == 3 @ d6.explode(depth=4)


def test_map_to_pool():
assert d6.pool(3).expand().map_to_pool().expand() == d6.pool(3).expand()

0 comments on commit 99dbf38

Please sign in to comment.