Skip to content

Commit

Permalink
Simplfy checking for patterns
Browse files Browse the repository at this point in the history
Replace `pat.get_head_name() == "System`Pattern"` with isinstance(pat, Pattern)
  • Loading branch information
rocky committed Sep 19, 2024
1 parent 77db22f commit 58e9def
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions mathics/core/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ def match_expression_with_one_identity(

# This tries to reduce the pattern to a non empty
# set of default values, and a single pattern.
from mathics.builtin.patterns import Pattern

self: ExpressionPattern = parms["self"]
vars_dict: dict = parms["vars_dict"]
Expand All @@ -721,7 +722,7 @@ def match_expression_with_one_identity(
elif pat_elem.get_head_name() == "System`Optional":
if len(pat_elem.elements) == 2:
pat, value = pat_elem.elements
if pat.get_head_name() == "System`Pattern":
if isinstance(pat, Pattern):
key = pat.elements[0].atom.name
else:
# if the first element of the Optional
Expand All @@ -731,7 +732,7 @@ def match_expression_with_one_identity(
optionals[key] = value
elif len(pat_elem.elements) == 1:
pat = pat_elem.elements[0]
if pat.get_head_name() == "System`Pattern":
if isinstance(pat, Pattern):
key = pat.elements[0].atom.name
else:
key = ""
Expand Down Expand Up @@ -893,12 +894,13 @@ def expression_pattern_match_element_orderless(
# otherwise, constructing a set() is very slow for large lists.
# performance test case:
# x = Range[100000]; Timing[Combinatorica`BinarySearch[x, 100]]
from mathics.builtin.patterns import Pattern

element: BaseElement = parms["element"]
element_candidates = set(element_candidates) # for fast lookup

sets = None
if element.get_head_name() == "System`Pattern":
if isinstance(element, Pattern):
varname = element.elements[0].get_name()
existing = parms["vars_dict"].get(varname, None)
if existing is not None:
Expand Down

0 comments on commit 58e9def

Please sign in to comment.