Share your thoughts in the 2021 Clojure Community Survey!

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

+2 votes
in Docs by
recategorized by

The docstrings for keys and vals indicate these functions can be applied to maps. But, these functions also evidently work on sequences of map entries.

For example, this works:

(keys (filter (comp odd? val) {:a 1 :b 2 :c 3}))

Is the above program valid? Are keys and vals guaranteed to work on sequences of map entries?

2 Answers

+2 votes
selected by
Best answer

Yes, this is legal and you can rely on it.

I also logged a ticket to either update the docstring or even make this more explicit in the types:

Can we expect `KVReduce` protocol? `(reduce-kv assoc {} (filter (comp odd? val) {:a 1}))`
There is an IKVReduce protocol already. The issue is that Seqable or ISeq cannot generically be extended as not all colls or seqs are KV. But there are places (like IPersistentMap) that could be marked as EntrySeqable (seqs to entries) and that would potentially be useful. We have run into this multiple times.
0 votes

From Alex Miller via Twitter.

“ it is legal and we will maintain that functionality going forward”