Clojure - Reference Values


Advertisements

Reference values are another way Clojure can work with the demand to have mutable variables. Clojure provides mutable data types such as atoms, agents, and reference types.

Following are the operations available for reference values.

Sr.No. Operations & Description
1 ref

This is used to create a reference value. When creating a reference value, there is an option to provide a validator function, which will validate the value created.

2 ref-set

This function is used to set the value of a reference to a new value irrespective of whatever is the older value.

3 alter

This function is used to alter the value of a reference type but in a safe manner. This is run in a thread, which cannot be accessed by another process.

4 dosync

Runs the expression (in an implicit do) in a transaction that encompasses expression and any nested calls.

5 commute

Commute is also used to change the value of a reference type just like alter and ref-set.

Advertisements