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
by
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:

https://clojure.atlassian.net/browse/CLJ-2592

by
Can we expect `KVReduce` protocol? `(reduce-kv assoc {} (filter (comp odd? val) {:a 1}))`
by
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
by

From Alex Miller via Twitter.

https://twitter.com/puredanger/status/1330570384773681152?s=21

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

...