Пример #1
0
def fib(n):
    """
    An example of making multiple recursive calls
    """
    if n in [0, 1]:
        retire(n)
    retire((yield n - 1) + (yield n - 2))
Пример #2
0
    def encode(self, pair):
        """
        encode an object as a python primitive

        Input may be a:
        - primitive in which case nothing is done to it
        - (type, parts) pair in which case the parts are recursively
          decomposed/encoded and a dict is returned
        """
        if not isinstance(pair, tuple):
            retire(pair)

        o_type, o = pair
        decomposed = type(o)()

        if hasattr(o, 'items'):
            for key, part in o.items():
                decomposed[key] = (yield part)
        else:
            for part in o:
                decomposed.append((yield part))

        retire({
            'type': self.encode_type(o_type),
            'parts': decomposed,
        })
Пример #3
0
def factorial(n):
    """
    A recursive definition of factorial with no explicit recursion depth limit
    """
    if n == 0:
        retire(1)
    retire(n * (yield n - 1))
Пример #4
0
 def lisp_eval(expression):
     """
     If the expression denotes a call, make a recursive call to the apply
     function otherwise return the expression
     """
     if isinstance(expression, tuple):
         retire((yield expression))
     retire(expression)
Пример #5
0
 def decode(self, encoded_o):
     """
     decode a dict-encoded object
     """
     if not isinstance(encoded_o, dict):
         retire(encoded_o)
     decoded = (self.decode_type(encoded_o['type']), encoded_o['parts'])
     retire((yield decoded))
Пример #6
0
 def lisp_apply(expression):
     """
     Make a recursive call to the eval function for each part in the
     expression and make the corresponding function call
     """
     evaluated = []
     for part in expression:
         evaluated.append((yield part))
     retire(evaluated[0](*evaluated[1:]))
Пример #7
0
 def decompose(self, o):
     """
     decompose an object into its constituent parts
     """
     action = self.get_action(o)
     if action is None:
         retire(o)
     else:
         retire((yield (type(o), self._do_action(action, o))))
Пример #8
0
    def compose(self, pair):
        """
        recompose a decomposed object
        """
        if not isinstance(pair, tuple):
            retire(pair)

        o_type, o = pair
        unencoded = type(o)()

        if isinstance(o, dict):
            for key, part in o.items():
                unencoded[key] = (yield part)
            retire(o_type(**unencoded))
        else:
            for part in o:
                unencoded.append((yield part))
            retire(o_type(unencoded))