- Clojure Tutorial
- Clojure - Home
- Clojure - Overview
- Clojure - Environment
- Clojure - Basic Syntax
- Clojure - REPL
- Clojure - Data Types
- Clojure - Variables
- Clojure - Operators
- Clojure - Loops
- Clojure - Decision Making
- Clojure - Functions
- Clojure - Numbers
- Clojure - Recursion
- Clojure - File I/O
- Clojure - Strings
- Clojure - Lists
- Clojure - Sets
- Clojure - Vectors
- Clojure - Maps
- Clojure - Namespaces
- Clojure - Exception Handling
- Clojure - Sequences
- Clojure - Regular Expressions
- Clojure - Predicates
- Clojure - Destructuring
- Clojure - Date & Time
- Clojure - Atoms
- Clojure - Metadata
- Clojure - StructMaps
- Clojure - Agents
- Clojure - Watchers
- Clojure - Macros
- Clojure - Reference Values
- Clojure - Databases
- Clojure - Java Interface
- Clojure - Concurrent Programming
- Clojure - Applications
- Clojure - Automated Testing
- Clojure - Libraries
- Clojure Useful Resources
- Clojure - Quick Guide
- Clojure - Useful Resources
- Clojure - Discussion
Clojure - commute
Commute is also used to change the value of a reference type just like alter and ref-set. The only difference is that this also needs to be placed inside a ‘dosync’ block. However, this can be used whenever there is no need to know which calling process actually changed the value of the reference type. Commute also uses a function to change the value of the reference variable.
Syntax
Following is the syntax.
(commute refname fun)
Parameters − ‘refname’ is the name of the variable holding the reference value. ‘fun’ is the function which is used to change the value of the reference type.
Return Value − The reference and its corresponding new value.
Example
An example on how this is used is shown in the following program.
(ns clojure.examples.example (:gen-class)) (defn Example [] (def counter (ref 0)) (defn change [counter] (dosync (commute counter inc))) (change counter) (println @counter) (change counter) (println @counter)) (Example)
Output
The above program produces the following output.
1 2
clojure_reference_values.htm
Advertisements