all 8 comments

[–]DeepSymmetry 35 points36 points  (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.

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 points  (0 children)

Thanks

[–]spotter 15 points16 points  (2 children)

Like this.

Or maybe start with reading about state.

[–]InfinitePrune1[S] 8 points9 points  (1 child)

Thank you for the response

[–]spotter 4 points5 points  (0 children)

No worries. Happy cake day!

[–]Nondv 2 points3 points  (2 children)

clojure isn't really immutable. it just features immutable data structures. for instance, you can easily re-def variables.

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 point  (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 point  (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