--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
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 clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
I'm not sure I agree with the intent of the patch (as I can certainly imagine times when I'd want a pure map from select-keys, not one that has the characteristics of the source map).
--
If you think of select-keys as dissoc'ing all the irrelevant keys, that would preserve the type of sorted-map, priority-map, but it would also turn a record into a regular map (because as soon as you dissoc a required key from a record, it turns into a plain map).Having select-keys on records returning regular maps is *not* inconsistent with preserving the "type" of map in the sense of sorted-map, priority-map, etc.Here's why:
If you think of select-keys as emptying out *all* the keys, and then building back up with the selected keys (a more accurate representation of what's going on under the covers), it still lends itself to exactly the same model. Emptying out the keys preserves the type of a map, but converts records to regular maps (because that's what already happens when you get rid of required keys from a record).
I think of it as creating a new map and filling it with entries from any keys in the specified set that exist in the map (which is essentially what it does now). My bias is purely that I'm used to and comfortable with that behavior.
Example? I can't think of one. Any key/value pair you retrieve from any map can be put in a regular map, right? Note that the current behavior is to do exactly this.
Otherwise, maintain the record type -- the core record contents are preserved, and essentially you only need to call select-keys only on the extended map of extra keys stored in the record.If any of the original record keys do not appear in the selected keys, then, as with dissoc, we must revert back to a map as there is no valid way to represent this as a record.To implement this for records:The two mental models predict somewhat differently what would happen when working with records. Records can hold additional keys. So imagine someone dumps a whole lot of additional keys, and then wants to "recover" the original, true record, by calling select-keys with the original keys in the record. I think this is a use case worth supporting. This corresponds to the mental model of discarding unwanted keys.I seem to be a relatively lone voice thinking it makes sense to preserve the type of the map, and honestly, I'm not going to lose any sleep if this patch is never actually implemented, but here's my two cents on the "optimal" design for records.Earlier in the thread, I mentioned two mental models about how select-keys works. Even though the actual implementation involves starting from an empty map and building back up, I think the behavior of select-keys should mimic the mental model of discarding irrelevant keys.
(defn select-keys++"Returns a new map of the same (hashed/sorted) type,that contains only those entries whose key is in keyseq"[map keyseq](apply dissocmap(clojure.set/difference (set (keys map))(set keyseq))))
(defn dissoc"dissoc[iate]. Returns a new map of the same (hashed/sorted) type,that does not contain a mapping for key(s).”(defn select-keys"Returns a map containing only those entries in map whose key is in keys"