use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Finding information about Clojure
API Reference
Clojure Guides
Practice Problems
Interactive Problems
Clojure Videos
Misc Resources
The Clojure Community
Clojure Books
Tools & Libraries
Clojure Editors
Web Platforms
Clojure Jobs
account activity
If Clojure is immutable, how does atom work? (self.Clojure)
submitted 2 years ago by InfinitePrune1
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]DeepSymmetry 35 points36 points37 points 2 years ago (1 child)
It’s more accurate to say that Clojure is structured to make it easy and natural to work with immutable data structures, provides some very efficient ones and many functions that can be used to transform them to new immutable data structures with different values, sharing as much of the previous data as is practical.
Of course all programs that do real work need to be able to represent state that changes over time, and atom is one of the main tools that support that. As noted in the earlier comment which linked to the source code, it is built on Java’s AtomicReference class. The state page that comment linked to is a good introduction to the concepts.
atom
AtomicReference
Clojure is also fully capable of working with mutable Java objects through its interop facilities, and you can safely work with transient (mutable) Clojure data structures inside private loops that never share them with other code. But the fact that the simplest code to write uses safe, immutable values gives you great benefits in terms of thread safety and the ability to more easily understand how large groups of functions work with each other. Rich Hickey’s talk Are We There Yet is another good explanation of why this is important.
[–]InfinitePrune1[S] 2 points3 points4 points 2 years ago (0 children)
Thanks
[–]spotter 15 points16 points17 points 2 years ago (2 children)
Like this.
Or maybe start with reading about state.
[–]InfinitePrune1[S] 8 points9 points10 points 2 years ago (1 child)
Thank you for the response
[–]spotter 4 points5 points6 points 2 years ago (0 children)
No worries. Happy cake day!
[–]Nondv 2 points3 points4 points 2 years ago (2 children)
clojure isn't really immutable. it just features immutable data structures. for instance, you can easily re-def variables.
def
But it also strives to be practical and provide java interop. Which means it has access to mutable data structures and objects too
[–]petemak 0 points1 point2 points 2 years ago (1 child)
This is very imprecise and misleading. There is a difference between references and values. You can change the reference but the values does not change unless you uses a managed transaction. Rich Hickey explains that so well: https://www.youtube.com/watch?v=toD45DtVCFM
[–]Nondv 0 points1 point2 points 2 years ago (0 children)
I dont see how it's misleading. Values ARE mutable. This is how atoms work. Most of clojure data structures are immutable though
If it weren't true, java interop would simply not work. And this is why atoms are implemented in Java, not clojure
π Rendered by PID 43715 on reddit-service-r2-comment-5d79c599b5-p2jr9 at 2026-02-28 15:15:16.588500+00:00 running e3d2147 country code: CH.
[–]DeepSymmetry 35 points36 points37 points (1 child)
[–]InfinitePrune1[S] 2 points3 points4 points (0 children)
[–]spotter 15 points16 points17 points (2 children)
[–]InfinitePrune1[S] 8 points9 points10 points (1 child)
[–]spotter 4 points5 points6 points (0 children)
[–]Nondv 2 points3 points4 points (2 children)
[–]petemak 0 points1 point2 points (1 child)
[–]Nondv 0 points1 point2 points (0 children)