<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def filter(self, limit=None, to=None, category=None):
 """ Returns the events that match the filters Args: limit (int, optional):
 the max length of the events to return (Default value = None) to (str, optional):
 only events that have been sent or received by 'to' (Default value = None) category (str, optional):
 only events belonging to the category (Default value = None) Returns: list: a list of filtered events """

        if category and not to:
            msg_slice = itertools.islice((x for x in self.store if x[2] == category), limit)
        elif to and not category:
            to = JID.fromstr(to)
            msg_slice = itertools.islice((x for x in self.store if _agent_in_msg(to, x[1])), limit)
        elif to and category:
            to = JID.fromstr(to)
            msg_slice = itertools.islice((x for x in self.store if _agent_in_msg(to, x[1]) and x[2] == category), limit)
        else:
            msg_slice = self.all(limit=limit)
            return msg_slice

        return list(msg_slice)[::-1]