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

0 votes
in Collections by

The vector returned by subvec has not metadata even when the original vector has metadata.


(meta (subvec 
       (with-meta [0 1 2 3 4 5] {:foo 42})
;; nil

Expected value:

 {:foo 42}

Live demo here.

1 Answer

0 votes

Having thought about it some, I think this is expected behavior. Generally metadata is preserved when using an "update" operation on a collection (assoc, dissoc, conj, disj). In this case, subvec is conceptually creating a new collection that is a subset of the old one (select-keys is maybe another in this category). Because it is a new collection, we would not expect it to get a copy of the metadata from the original.

What about the edge case where subvec receives `0` and returns the whole vector?

Also, select-keys preserve metadata.