persistent! should be called on the return value of assoc! not on t.
I find all the transient / persistent stuff makes the code feel a lot more complex cos it's adding quite a bit of cognitive load
@ordnungswidrig who is obviously a Clojure wiz took 2 tries and @mpenet has issued a correction
Transients can be quite faster, but the api is exactly the same as the normal data structures - you have to use the return value of assoc!, you donβt mutate in place.
Good morning! I know I'm lazy, but it seems to be the right place to ask what transients are about and why they can be quite faster. I won't take offence if you tell me to google it or just give me a link. π
@orestis the documentation suggests otherwise :thinking_face:
Transients are mutable versions of collections. They are used when you do a lot of mutation within a single function, but you are not supposed to share them outside.
> Transients support a parallel set of 'changing' operations, with similar names followed by ! - assoc!, conj! etc. These do the same things as their persistent counterparts except the return values are themselves transient. Note in particular that transients are not designed to be bashed in-place. You must capture and use the return value in the next call. In this way, they support the same code structure as the functional persistent code they replace. As the example will show, this will allow you to easily enhance the performance of a piece of code without structural change.
The guide is very nicely written: https://clojure.org/reference/transients
@orestis ok, I stand corrected. The documentation uses a loop which I interpreted wrongly. You were correct, use the return value. (I wonder what happens if you don't)
Thanks @orestis! Much appreciated. So sometimes when I reach for Java data structures for performance reasons I could also consider transients and keep the code looking someone similar to what it would look like using the immutable structures, is that a somewhat correct take on them?
@pez exactly correct but you have to be careful to not use transients anywhere with threads or anywhere with the assumption of using a persistent data structure. You would usually call transient! at the top of a function and persistent! at the exit.
Most uses of transients I see in the wild are wrong... Either bashing in place or poor substitutes for a simple into
call (which will use transients under the hood). There are valid uses cases but most often there's a higher level construct just as good.
> I find all the transient / persistent stuff makes the code feel a lot more complex cos it's adding quite a bit of cognitive load Go figure what would happen if you would write programs directly in Java...
lol
Goodnight
Terrifying corn π½
Corn is transient
Trying to stay on topic ;-)