<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def apply(self, data):
 """ Apply the given mapping to ``data``, recursively. The return type is a tuple of a boolean and the resulting data element. The boolean indicates whether any values were mapped in the child nodes of the mapping. It is used to skip optional branches of the object graph. """

        if self.visitor.is_object:
            obj = {}
            if self.visitor.parent is None:
                obj['$schema'] = self.visitor.path
            obj_empty = True
            for child in self.children:
                empty, value = child.apply(data)
                if empty and child.optional:
                    continue
                obj_empty = False if not empty else obj_empty

                if child.visitor.name in obj and child.visitor.is_array:
                    obj[child.visitor.name].extend(value)
                else:
                    obj[child.visitor.name] = value
            return obj_empty, obj

        elif self.visitor.is_array:
            empty, value = self.children.apply(data)
            return empty, [value]

        elif self.visitor.is_value:
            return extract_value(self.mapping, self.visitor, data)