Skip to content

Commit

Permalink
More minor corrections ...
Browse files Browse the repository at this point in the history
See conversation tab in #1110
  • Loading branch information
rocky committed Oct 3, 2024
1 parent 00233ca commit 27c8bc1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
12 changes: 9 additions & 3 deletions mathics/builtin/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
SymbolBlank,
SymbolDefault,
SymbolDispatch,
SymbolInfinity,
SymbolRule,
SymbolRuleDelayed,
)
Expand Down Expand Up @@ -523,7 +524,11 @@ def eval(
) -> OptionalType[BaseElement]:
"ReplaceList[expr_, rules_, maxidx_:Infinity]"

if maxidx.get_name() == "System`Infinity":
# TODO: the below handles Infinity getting added as a
# default argument, when it is passed explitly, e.g.
# ReplaceList[expr, {}, Infinity], then Infinity
# comes in as DirectedInfinity[1].
if maxidx == SymbolInfinity:
max_count = None
else:
max_count = maxidx.get_int_value()
Expand Down Expand Up @@ -625,6 +630,7 @@ def yield_match(vars_2, rest):
else:
yield_func(vars_2, None)

# TODO: clarify why we need to use copy here.
pattern_context = pattern_context.copy()
pattern_context["yield_func"] = yield_match
self.pattern.match(expression, pattern_context)
Expand Down Expand Up @@ -869,12 +875,12 @@ def match(self, expression: Expression, pattern_context: dict):

def get_match_count(
self, vars_dict: OptionalType[dict] = None
) -> Union[int, tuple]:
) -> Union[None, int, tuple]:
range_lst = None
for alternative in self.alternatives:
sub = alternative.get_match_count(vars_dict)
if range_lst is None:
range_lst = list(sub)
range_lst = tuple(sub)
else:
if sub[0] < range_lst[0]:
range_lst[0] = sub[0]
Expand Down
14 changes: 7 additions & 7 deletions mathics/core/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
expressions, patterns are also used in applying of transformation
rules and deciding functions that get applied.
See also: mathics.core.rules and
See also: mathics.core.rules and
https://reference.wolfram.com/language/tutorial/PatternsAndTransformationRules.html
"""

Expand Down Expand Up @@ -364,7 +364,7 @@ def get_match_candidates(
)
)

def get_match_count(self, vars_dict: Optional[dict] = None) -> Union[int, tuple]:
def get_match_count(self, vars_dict: Optional[dict] = None) -> Tuple[int, int]:
"""The number of matches"""
return (1, 1)

Expand Down Expand Up @@ -482,7 +482,7 @@ def filter_elements(self, head_name: str):
def __repr__(self):
return f"<ExpressionPattern: {self.expr}>"

def get_match_count(self, vars_dict: Optional[dict] = None):
def get_match_count(self, vars_dict: Optional[dict] = None) -> Tuple[int, int]:
"""the number of matches"""
return (1, 1)

Expand All @@ -492,9 +492,9 @@ def get_wrappings(self, yield_func: Callable, items: Tuple, pattern_context: dic
If items has length 1, apply yield_func to the unique element.
Otherwise, apply it to a sequence. If the expression has the
attribute "Orderless", apply it to all the possible orders.
Finally , if the expression is Flat, and the parameter `include_flattened`
is True, apply yield_func to the expression with the head of the original
attribute `Orderless`, apply it to all the possible orders.
Finally , if the expression is `Flat`, and the parameter `include_flattened`
is `True`, apply yield_func to the expression with the head of the original
expression applied to the original sequence.
"""
if len(items) == 1:
Expand Down Expand Up @@ -940,7 +940,7 @@ def expression_pattern_match_element_process_items(
parms: dict,
):
"""
try to match sequences built from items
Try to match sequences built from items
against the pattern.
"""
# Include wrappings like Plus[a, b] only if not all items taken
Expand Down

0 comments on commit 27c8bc1

Please sign in to comment.