Skip to content

clojure.core/swap!

View this page on ClojureDocs

Type: function Added: Clojure 1.0 Examples: 6 Runnable: 6
([atom f] [atom f x] [atom f x y] [atom f x y & args])

Atomically swaps the value of atom to be: (apply f current-value-of-atom args). Note that f may be called multiple times, and thus should be free of side effects. Returns the value that was swapped in.

Examples

by Kototama on
by Kototama on
by esumitra on
by ghiden on
by ibercode on
by Kaspazza on

Note by amirrajan

If you want to swap the entire atom, user reset!.

Note by KyleErhabor

When using swap!, it's important you're only taking into consideration the value of the atom inside f. See https://ericnormand.me/mini-guide/atom-code-explanation for more information.

See also


Content from the matching ClojureDocs page, with authors credited on each contribution.