<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def swap(self, fn, *args, **kwargs):
        '''
        Given a mutator `fn`, calls `fn` with the atom's current state, `args`,
        and `kwargs`. The return value of this invocation becomes the new value
        of the atom. Returns the new value.

        :param fn: A function which will be passed the current state. Should
            return a new state. This absolutely *MUST NOT* mutate the
            reference to the current state! If it does, this function may loop
            indefinitely.
        :param \*args: Arguments to be passed to `fn`.
        :param \*\*kwargs: Keyword arguments to be passed to `fn`.
        '''
        while True:
            oldval = self.deref()
            newval = fn(oldval, *args, **kwargs)
            if self._state.compare_and_set(oldval, newval):
                self.notify_watches(oldval, newval)
                return newval