Fork me on GitHub
#clojurescript
<
2021-10-06
>
cjsauer17:10:12

I have a strange question that might not make sense, but does cljs have a way to check whether two given immutable values share the same “parent” in its structural sharing “chain”?

(def m {:a 1})
(common-parent? m (assoc m :b 1))
;; => true
I realize this is a really weird ask, and probably not something I should be doing 😅

thheller18:10:23

although you could use metadata to track this (def m ^{:foo 1} {:a 1}) (meta (assoc m :b 1)). as long as you use assoc/dissoc/update and stuff the metadata will remain

👆 1
cjsauer20:10:28

Yea that’s interesting. Maybe store some kind of generated id in the metadata for comparison later:

(def m ^{:id (str (gensym))} ...)

bbss02:10:30

perhaps clojure.data/diff could still be useful for your goals!

cjsauer03:10:42

I’ll check it out, thanks!

raspasov05:10:27

It seems like metadata can probably help you. Just watch out for all the gotchas in the cases where metadata can disappear.

cjsauer15:10:03

Yea definitely. Basically sticking to core functions and never accidentally recreating values from scratch? I’ve noticed in UI dev, even without metadata involved, I tend to think about the lifetimes of my values to ensure that prop diff checking remains very fast. I imagine metadata survival is a similar problem.

👌 1
thheller18:10:01

@cjsauer no. in your example you'd get array maps where the array is just cloned and the resulting map maintains no relation to its "parent"

cjsauer18:10:44

Ok thank you, makes sense.