Mutable References

Learn about the variables and references in Clojure that change their content over time.

Introduction

Clojure can be considered one of the safest languages when dealing with concurrency. As in any other language, sometimes we need to perform state-modification operations concurrently. Until now, we’ve talked a lot about immutability and how things are expected to happen in Clojure. However, at some point, we need to have a functionality to deal with states.

The term value is commonly used among the Clojure community, but its specific meaning might differ from what we’re used to. Values are considered atomic in the way that they form a unit or component in a larger system. For instance, numbers are values: it wouldn’t make sense for the number 13 to mutate into another number. When we apply a mathematical function for adding or subtracting from 13, we don’t change the number 13; we just create a new value with a different number.

Clojure’s data structures are all values because they’re immutable. When we use assoc on a map, we don’t modify the original ...

Access this course and 1400+ top-rated courses and projects.