Skip to content

Commit

Permalink
add default argument to lowest(), highest(), middle()
Browse files Browse the repository at this point in the history
  • Loading branch information
HighDiceRoller committed Nov 13, 2023
1 parent f9fbbc0 commit 81d3a38
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions src/icepool/population/keep.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ def lowest(iterable: 'Iterable[T | icepool.Die[T]]', /) -> 'icepool.Die[T]':


@overload
def lowest(arg0: 'T | icepool.Die[T]', arg1: 'T | icepool.Die[T]', /,
*args: 'T | icepool.Die[T]') -> 'icepool.Die[T]':
def lowest(arg0: 'T | icepool.Die[T]', arg1: 'T | icepool.Die[T]', /, *args:
'T | icepool.Die[T]') -> 'icepool.Die[T]':
...


def lowest(arg0,
/,
*more_args: 'T | icepool.Die[T]',
keep: int = 1,
drop: int = 0) -> 'icepool.Die[T]':
drop: int = 0,
default: T | None = None) -> 'icepool.Die[T]':
"""The lowest outcome among the rolls, or the sum of some of the lowest.
The outcomes should support addition and multiplication if `keep != 1`.
Expand All @@ -38,7 +39,15 @@ def lowest(arg0,
if len(more_args) == 0:
args = arg0
else:
args = (arg0,) + more_args
args = (arg0, ) + more_args

if len(args) == 0:
if default is None:
raise ValueError(
"lowest() arg is an empty sequence and no default was provided."
)
else:
return icepool.Die([default])

if keep < 0:
raise ValueError(f'keep={keep} cannot be negative.')
Expand All @@ -56,16 +65,17 @@ def highest(iterable: 'Iterable[T | icepool.Die[T]]', /) -> 'icepool.Die[T]':


@overload
def highest(arg0: 'T | icepool.Die[T]', arg1: 'T | icepool.Die[T]', /,
*args: 'T | icepool.Die[T]') -> 'icepool.Die[T]':
def highest(arg0: 'T | icepool.Die[T]', arg1: 'T | icepool.Die[T]', /, *args:
'T | icepool.Die[T]') -> 'icepool.Die[T]':
...


def highest(arg0,
/,
*more_args: 'T | icepool.Die[T]',
keep: int = 1,
drop: int = 0) -> 'icepool.Die[T]':
drop: int = 0,
default: T | None = None) -> 'icepool.Die[T]':
"""The highest outcome among the rolls, or the sum of some of the highest.
The outcomes should support addition and multiplication if `keep != 1`.
Expand All @@ -80,7 +90,15 @@ def highest(arg0,
if len(more_args) == 0:
args = arg0
else:
args = (arg0,) + more_args
args = (arg0, ) + more_args

if len(args) == 0:
if default is None:
raise ValueError(
"highest() arg is an empty sequence and no default was provided."
)
else:
return icepool.Die([default])

if keep < 0:
raise ValueError(f'keep={keep} cannot be negative.')
Expand All @@ -98,16 +116,17 @@ def middle(iterable: 'Iterable[T | icepool.Die[T]]', /) -> 'icepool.Die[T]':


@overload
def middle(arg0: 'T | icepool.Die[T]', arg1: 'T | icepool.Die[T]', /,
*args: 'T | icepool.Die[T]') -> 'icepool.Die[T]':
def middle(arg0: 'T | icepool.Die[T]', arg1: 'T | icepool.Die[T]', /, *args:
'T | icepool.Die[T]') -> 'icepool.Die[T]':
...


def middle(arg0,
/,
*more_args: 'T | icepool.Die[T]',
keep: int = 1,
tie: Literal['error', 'high', 'low'] = 'error') -> 'icepool.Die[T]':
tie: Literal['error', 'high', 'low'] = 'error',
default: T | None = None) -> 'icepool.Die[T]':
"""The middle of the outcomes among the rolls, or the sum of some of the middle.
The outcomes should support addition and multiplication if `keep != 1`.
Expand All @@ -125,7 +144,16 @@ def middle(arg0,
if len(more_args) == 0:
args = arg0
else:
args = (arg0,) + more_args
args = (arg0, ) + more_args

if len(args) == 0:
if default is None:
raise ValueError(
"middle() arg is an empty sequence and no default was provided."
)
else:
return icepool.Die([default])

# Expression evaluators are difficult to type.
return icepool.Pool(args).middle(keep, tie=tie).sum() # type: ignore

Expand Down

0 comments on commit 81d3a38

Please sign in to comment.