Example #1
0
    def apply_levelspec(self, expr, rules, ls, evaluation, options):
        'Replace[expr_, rules_, Optional[Pattern[ls, _?LevelQ], {0}], OptionsPattern[Replace]]'
        try:
            rules, ret = create_rules(rules, expr, 'Replace', evaluation)
            if ret:
                return rules

            heads = self.get_option(options, 'Heads', evaluation).is_true()

            result, applied = expr.apply_rules(
                rules, evaluation, level=0, options={
                    'levelspec': python_levelspec(ls), 'heads': heads})
            return result
        except InvalidLevelspecError:
            evaluation.message('General', 'level', ls)
Example #2
0
 def apply_level(self, f, expr, ls, evaluation, options={}):
     'Map[f_, expr_, ls_?LevelQ:{1}, OptionsPattern[Map]]'
     
     try:
         start, stop = python_levelspec(ls)
     except InvalidLevelspecError:
         evaluation.message('Map', 'level', ls)
         return
     
     def callback(level):
         return Expression(f, level)
     
     heads = self.get_option(options, 'Heads', evaluation).is_true()
     result, depth = walk_levels(expr, start, stop, heads=heads, callback=callback)
     
     return result
Example #3
0
 def apply_level(self, f, expr, ls, evaluation, options={}):
     'MapIndexed[f_, expr_, ls_?LevelQ:{1}, OptionsPattern[MapIndexed]]'
     
     try:
         start, stop = python_levelspec(ls)
     except InvalidLevelspecError:
         evaluation.message('MapIndexed', 'level', ls)
         return
     
     def callback(level, pos):
         return Expression(f, level, Expression('List', *(Integer(p) for p in pos)))
     
     heads = self.get_option(options, 'Heads', evaluation).is_true()
     result, depth = walk_levels(expr, start, stop, heads=heads, callback=callback, include_pos=True)
     
     return result
Example #4
0
    def apply_level(self, f, expr, ls, evaluation, options={}):
        """Map[f_, expr_, Optional[Pattern[ls, _?LevelQ], {1}],
                OptionsPattern[Map]]"""

        try:
            start, stop = python_levelspec(ls)
        except InvalidLevelspecError:
            evaluation.message("Map", "level", ls)
            return

        def callback(level):
            return Expression(f, level)

        heads = self.get_option(options, "Heads", evaluation).is_true()
        result, depth = walk_levels(expr, start, stop, heads=heads, callback=callback)

        return result
Example #5
0
    def apply_level(self, f, expr, ls, evaluation, options={}):
        """MapIndexed[f_, expr_, Optional[Pattern[ls, _?LevelQ], {1}],
                OptionsPattern[MapIndexed]]"""

        try:
            start, stop = python_levelspec(ls)
        except InvalidLevelspecError:
            evaluation.message("MapIndexed", "level", ls)
            return

        def callback(level, pos):
            return Expression(f, level, Expression("List", *[Integer(p) for p in pos]))

        heads = self.get_option(options, "Heads", evaluation).is_true()
        result, depth = walk_levels(expr, start, stop, heads=heads, callback=callback, include_pos=True)

        return result
Example #6
0
 def apply(self, f, expr, ls, evaluation, options={}):
     'Apply[f_, expr_, ls_?LevelQ:{0}, OptionsPattern[Apply]]'
     
     try:
         start, stop = python_levelspec(ls)
     except InvalidLevelspecError:
         evaluation.message('Apply', 'level', ls)
         return
     
     def callback(level):
         if level.is_atom():
             return level
         else:
             return Expression(f, *level.leaves)
     
     heads = self.get_option(options, 'Heads', evaluation).is_true()
     result, depth = walk_levels(expr, start, stop, heads=heads, callback=callback)
     
     return result
Example #7
0
    def apply_level(self, f, expr, ls, evaluation, options={}):
        '''Scan[f_, expr_, Optional[Pattern[ls, _?LevelQ], {1}],
                OptionsPattern[Map]]'''

        try:
            start, stop = python_levelspec(ls)
        except InvalidLevelspecError:
            evaluation.message('Map', 'level', ls)
            return

        def callback(level):
            Expression(f, level).evaluate(evaluation)
            return level

        heads = self.get_option(options, 'Heads', evaluation).is_true()
        result, depth = walk_levels(
            expr, start, stop, heads=heads, callback=callback)

        return Symbol('Null')
Example #8
0
    def apply(self, expr, test, level, evaluation):
        "%(name)s[expr_, test_, level_]"

        try:
            start, stop = python_levelspec(level)
        except InvalidLevelspecError:
            evaluation.message("Level", "level", level)
            return

        def callback(node):
            self._short_circuit(Expression(test, node).evaluate(evaluation).is_true())
            return node

        try:
            walk_levels(expr, start, stop, callback=callback)
        except _ShortCircuit as e:
            return e.result

        return self._no_short_circuit()
Example #9
0
    def apply(self, f, expr, ls, evaluation, options={}):
        """Apply[f_, expr_, Optional[Pattern[ls, _?LevelQ], {0}],
                OptionsPattern[Apply]]"""

        try:
            start, stop = python_levelspec(ls)
        except InvalidLevelspecError:
            evaluation.message("Apply", "level", ls)
            return

        def callback(level):
            if level.is_atom():
                return level
            else:
                return Expression(f, *level.leaves)

        heads = self.get_option(options, "Heads", evaluation).is_true()
        result, depth = walk_levels(expr, start, stop, heads=heads, callback=callback)

        return result
Example #10
0
    def apply_level(self, f, expr, ls, evaluation, options={}):
        'MapIndexed[f_, expr_, ls_?LevelQ:{1}, OptionsPattern[MapIndexed]]'

        try:
            start, stop = python_levelspec(ls)
        except InvalidLevelspecError:
            evaluation.message('MapIndexed', 'level', ls)
            return

        def callback(level, pos):
            return Expression(f, level,
                              Expression('List', *(Integer(p) for p in pos)))

        heads = self.get_option(options, 'Heads', evaluation).is_true()
        result, depth = walk_levels(expr,
                                    start,
                                    stop,
                                    heads=heads,
                                    callback=callback,
                                    include_pos=True)

        return result
Example #11
0
    def apply_level(self, f, expr, ls, evaluation, options={}):
        """MapIndexed[f_, expr_, Optional[Pattern[ls, _?LevelQ], {1}],
        OptionsPattern[MapIndexed]]"""

        try:
            start, stop = python_levelspec(ls)
        except InvalidLevelspecError:
            evaluation.message("MapIndexed", "level", ls)
            return

        def callback(level, pos):
            return Expression(f, level,
                              Expression("List", *[Integer(p) for p in pos]))

        heads = self.get_option(options, "Heads", evaluation).is_true()
        result, depth = walk_levels(expr,
                                    start,
                                    stop,
                                    heads=heads,
                                    callback=callback,
                                    include_pos=True)

        return result
Example #12
0
    def apply(self, f, expr, ls, evaluation, options={}):
        'Apply[f_, expr_, ls_?LevelQ:{0}, OptionsPattern[Apply]]'

        try:
            start, stop = python_levelspec(ls)
        except InvalidLevelspecError:
            evaluation.message('Apply', 'level', ls)
            return

        def callback(level):
            if level.is_atom():
                return level
            else:
                return Expression(f, *level.leaves)

        heads = self.get_option(options, 'Heads', evaluation).is_true()
        result, depth = walk_levels(expr,
                                    start,
                                    stop,
                                    heads=heads,
                                    callback=callback)

        return result
Example #13
0
    def apply(self, f, expr, ls, evaluation, options={}):
        """Apply[f_, expr_, Optional[Pattern[ls, _?LevelQ], {0}],
        OptionsPattern[Apply]]"""

        try:
            start, stop = python_levelspec(ls)
        except InvalidLevelspecError:
            evaluation.message("Apply", "level", ls)
            return

        def callback(level):
            if level.is_atom():
                return level
            else:
                return Expression(f, *level.leaves)

        heads = self.get_option(options, "Heads", evaluation).is_true()
        result, depth = walk_levels(expr,
                                    start,
                                    stop,
                                    heads=heads,
                                    callback=callback)

        return result
Example #14
0
    def apply_levelspec(self, expr, rules, ls, evaluation, options):
        'Replace[expr_, rules_, Optional[Pattern[ls, _?LevelQ], {0}], OptionsPattern[Replace]]'
        try:
            rules, ret = create_rules(rules, expr, 'Replace', evaluation)
            if ret:
                return rules

            heads = self.get_option(options, 'Heads', evaluation).is_true()

            result, applied = expr.apply_rules(rules,
                                               evaluation,
                                               level=0,
                                               options={
                                                   'levelspec':
                                                   python_levelspec(ls),
                                                   'heads':
                                                   heads
                                               })
            return result
        except InvalidLevelspecError:
            evaluation.message('General', 'level', ls)

        except PatternError:
            evaluation.message('Replace', 'reps', rules)
Example #15
0
    def apply_levelspec(self, expr, rules, ls, evaluation, options):
        "Replace[expr_, rules_, Optional[Pattern[ls, _?LevelQ], {0}], OptionsPattern[Replace]]"
        try:
            rules, ret = create_rules(rules, expr, "Replace", evaluation)
            if ret:
                return rules

            heads = self.get_option(options, "Heads", evaluation).is_true()

            result, applied = expr.apply_rules(
                rules,
                evaluation,
                level=0,
                options={
                    "levelspec": python_levelspec(ls),
                    "heads": heads
                },
            )
            return result
        except InvalidLevelspecError:
            evaluation.message("General", "level", ls)

        except PatternError:
            evaluation.message("Replace", "reps", rules)