Welcome! Please see the About page for a little more info on how this works.

0 votes
in data.int-map by
retagged by

Shared empty int-map

I notice that in the contrib library clojure.data.int-map, a new instance of the empty PersistentIntMap is created every time (int-map) is called. This is different to how all the other persistent collections work, where there's only one single empty list/vector/map/set exists, and it is reused.

It's also the same situation with PersistentIntSet, both dense and non-dense.

(empty my-int-map) doesn't preserve meta

the IPersistentCollection#empty() implementation on PersistentIntMap is:

  (empty [this]
      (PersistentIntMap. Nodes$Empty/EMPTY 0 nil))

So the metadata from this is lost in the returned empty int-map. Other strict collection implementations retain metadata in the returned collection:

(mapv (fn [coll] (meta (empty (with-meta coll {:a 1})))) 
    [() [] {} #{} (sorted-map) (sorted-set)])
=> [{:a 1} {:a 1} {:a 1} {:a 1} {:a 1} {:a 1}]

The same is true for PersistentIntSet

1 Answer

+1 vote
selected by