Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should you use swap or reset

Tags:

What is the difference between using swap! and reset! in Clojure functions? I have seen from the clojure.core docs that they are used to change the value of an atom but I am not sure when to use swap! and when to use reset!.

What circumstances would you use swap! and which circumstances would you use reset!?

[:input {:type "text"
         :value @time-color
         :on-change #(reset! time-color (-> % .-target .-value))}]

The above code is an example of using reset! for a button

[:input.form-control
          {:type      :text
           :name      :ric
           :on-change #(swap! fields assoc :ric (-> % .-target .-value))
           :value     (:ric @fields)}]

And this button uses swap!

Are swap! and reset! interchangeable?

Thanks

like image 925
rbb Avatar asked Nov 28 '16 12:11

rbb


People also ask

What actually gets swapped in an interest rate swap?

In an interest rate swap, the only things that actually get swapped are the interest payments. An interest rate swap, as previously noted, is a derivative contract.

What is a swaption and how do I use it?

Use a Swaption: A swaption is an option on a swap. Purchasing a swaption would allow a party to set up, but not enter into, a potentially offsetting swap at the time they execute the original swap. This would reduce some of the market risks associated with Strategy 2.

Why do firms use swaps?

The motivations for using swap contracts fall into two basic categories: commercial needs and comparative advantage. The normal business operations of some firms lead to certain types of interest rate or currency exposures that swaps can alleviate.

What is the swap reset date and payment date?

Usually, the swap reset date precedes the payment date by the number of months in a reset period (three months, six months, etc). Occasionally, it happens that swaps trade on interim dates that do not correspond to calendar dates (swap reset dates and payment dates) that are set by market convention.


1 Answers

swap! uses a function to modify the value of the atom. You will usually use swap! when the current value of the atom matters. For example, incrementing a value depends on the current value, so you would use the inc function.

reset! simply sets the value of the atom to some new value. You will usually use this when you just want to set the value without caring what the current value is.

(def x (atom 0))
(swap! x inc)   ; @x is now 1
(reset! x 100)  ; @x is now 100
(swap! x inc)   ; @x is now 101
like image 134
Josh Avatar answered Sep 27 '22 23:09

Josh