alter updates a ref inside a transaction by applying a function to its current value. Place it inside dosync when coordinating changes across multiple refs. section 14.4
Use alter when:
- A function such as inc, +, or - computes the next ref value. section 14.4
- Several refs must change atomically within one dosync transaction. section 14.4
- Prefer ref-set when directly replacing a ref’s value instead of deriving it through a function. section 14.4
Coordinated account transfer
Define two refs and update them together: (dosync (alter account-1 - amount) (alter account-2 + amount)). Transferring 300 from accounts holding 1000 and 500 produces {:account-1 700, :account-2 800}.
