# swap! Apply a function to a ref cell's current value and update it. Returns the new value. Fires any registered watchers with `(key cell old new)`. ## Syntax
[swap! $a $f $args...]
## Parameters - `$a` — A ref cell (anonymous ref or `[var x]` binding cell). - `$f` — Function called as `($f $current $args...)`. - `$args...` — Extra args passed to `$f` after the current value. ## Examples
def counter [ref 0]
[swap! $counter {|n| ($n + 1)}]   # 1
[swap! $counter + 10]             # 11
## Returns The cell's new value (any type). ## Notes - The update function must not suspend (`await`/`sleep`/`yield`). Deft's scheduler is cooperative, so a non-suspending update is atomic; a suspending one is last-writer-wins — there is no compare-and-retry. Keep the function pure. ## See Also `reset!`, `deref`, `ref`, `add-watch`