Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedups for Position[], Cases[], DeleteCases[] #503

Merged
merged 1 commit into from
Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions mathics/builtin/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,10 +1381,19 @@ class Cases(Builtin):
= {2 I, 4 - I}
"""
rules = {
'Cases[list_, pattern_]': 'Select[list, MatchQ[#, pattern]&]',
'Cases[pattern_][list_]': 'Cases[list, pattern]',
}

def apply(self, items, pattern, evaluation):
'Cases[items_, pattern_]'
if items.is_atom():
evaluation.message('Select', 'normal')
return

from mathics.builtin.patterns import Matcher
match = Matcher(pattern).match
return Expression('List', *[leaf for leaf in items.leaves if match(leaf, evaluation)])


class DeleteCases(Builtin):
"""
Expand All @@ -1400,9 +1409,15 @@ class DeleteCases(Builtin):
= {1, 2, 3}
"""

rules = {
'DeleteCases[list_, pattern_]': 'Select[list, ! MatchQ[#, pattern]&]',
}
def apply(self, items, pattern, evaluation):
'DeleteCases[items_, pattern_]'
if items.is_atom():
evaluation.message('Select', 'normal')
return

from mathics.builtin.patterns import Matcher
match = Matcher(pattern).match
return Expression('List', *[leaf for leaf in items.leaves if not match(leaf, evaluation)])


class Position(Builtin):
Expand Down Expand Up @@ -1452,10 +1467,13 @@ def apply_level(self, expr, patt, ls, evaluation, options={}):
except InvalidLevelspecError:
return evaluation.message('Position', 'level', ls)

from mathics.builtin.patterns import Matcher

match = Matcher(patt).match
result = []

def callback(level, pos):
expr = Expression('MatchQ', level, patt)
if expr.evaluate(evaluation).is_true():
if match(level, evaluation):
result.append(pos)
return level

Expand Down
31 changes: 19 additions & 12 deletions mathics/builtin/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,19 +572,26 @@ def except_yield_func(vars, rest):
self.p.match(yield_func, expression, vars, evaluation)


class Matcher(object):
def __init__(self, form):
self.form = Pattern.create(form)

def match(self, expr, evaluation):
class StopGenerator_MatchQ(StopGenerator):
pass

def yield_func(vars, rest):
raise StopGenerator_MatchQ(Symbol("True"))

try:
self.form.match(yield_func, expr, {}, evaluation)
except StopGenerator_MatchQ:
return True
return False


def match(expr, form, evaluation):
class StopGenerator_MatchQ(StopGenerator):
pass

form = Pattern.create(form)

def yield_func(vars, rest):
raise StopGenerator_MatchQ(Symbol("True"))
try:
form.match(yield_func, expr, {}, evaluation)
except StopGenerator_MatchQ:
return True
return False
return Matcher(form).match(expr, evaluation)


class MatchQ(Builtin):
Expand Down