Are there common techniques or idioms for achieving structural sharing when
composing data where equivalences exist?
(My motivation is to reduce heap usage for a particular problem I'm working
on that involves a lot of repeated data.)
As a specific example, consider sharing equivalent map values:
(def m1 {:a [1 2]})
(def m2 (assoc m1 :b [1 2]))
(identical? (m2 :a) (m2 :b))
;; => false
For this simple example, I can force sharing by introducing a variant of
assoc which looks at existing values:
(defn assoc-id [map key val] (assoc map key (get (apply hash-set (vals
map)) val val)))
And using it results in a map with two identical values:
(def m3 (assoc-id m1 :b [1 2]))
(identical? (m3 :a) (m3 :b))
;; => true
But, of course, this approach could get to be expensive and/or unwieldy if
not done carefully.
So, I was wondering if there are general techniques in this area. I'm sure
I'm not the first to come across it. We already get this automatically for
boxing of small integers:
(identical? 5 5)
;; => true
(identical? 500 500)
;; => false
So I suppose I'm essentially looking for the same idea but for larger
non-primitive "values".
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.