Fork me on GitHub
#clojure-europe
<
2020-09-19
>
raymcdermott15:09:55

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

raymcdermott15:09:40

@ordnungswidrig who is obviously a Clojure wiz took 2 tries and @mpenet has issued a correction

orestis15:09:31

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.

pez16:09:33

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. 😃

ordnungswidrig16:09:44

@orestis the documentation suggests otherwise :thinking_face:

orestis16:09:46

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.

orestis16:09:11

> 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.

orestis16:09:35

The guide is very nicely written: https://clojure.org/reference/transients

👍 1
ordnungswidrig16:09:47

@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)

pez16:09:03

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?

orestis16:09:43

@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.

👍 2
mpenet17:09:17

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.

👍 3
borkdude20:09:08

> 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...

😂 1
otfrom21:09:53

Terrifying corn 🌽

raymcdermott22:09:37

Corn is transient

raymcdermott22:09:17

Trying to stay on topic ;-)