Clojure

map-invert should use (empty m) instead of {}

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Declined
  • Affects Version/s: Release 1.6
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

clojure.set/map-invert should reduce with (empty m) instead of {} so that it returns a map of the same type as its argument.

This is a trivial change and I'm willing to submit a patch if nobody opposes.

Activity

Hide
Alex Miller added a comment -

I don't think that always makes sense. Say you had a map of string to integers with a custom comparator created by sorted-map-by. If you use empty, you'd still have a map with a custom comparator which you would pour integer keys into and would likely throw a ClassCastException.

What is the use case that led you to this ticket?

Show
Alex Miller added a comment - I don't think that always makes sense. Say you had a map of string to integers with a custom comparator created by sorted-map-by. If you use empty, you'd still have a map with a custom comparator which you would pour integer keys into and would likely throw a ClassCastException. What is the use case that led you to this ticket?
Hide
Gregory Schlomoff added a comment -

Hello Alex, thanks for commenting.

My use case is that I have a custom type that implements IPersistentMap. If I use map-invert over it, I get a regular map back, which is problematic because regular maps don't allow multiple values for the same key, unlike my multimap implementation, so I loose information.

(map-invert (my-multimap :a 1, :b 1))
=> {1 :b} ; lost the (1 :a) entry because regular maps don't allow duplicate keys

Maybe a solution would be to make a version of map-invert that takes a map to insert the inverted entries into?

I'm not adamant over this, if you think there is no elegant solution for this issue we can close it.

Show
Gregory Schlomoff added a comment - Hello Alex, thanks for commenting. My use case is that I have a custom type that implements IPersistentMap. If I use map-invert over it, I get a regular map back, which is problematic because regular maps don't allow multiple values for the same key, unlike my multimap implementation, so I loose information. (map-invert (my-multimap :a 1, :b 1)) => {1 :b} ; lost the (1 :a) entry because regular maps don't allow duplicate keys Maybe a solution would be to make a version of map-invert that takes a map to insert the inverted entries into? I'm not adamant over this, if you think there is no elegant solution for this issue we can close it.
Hide
Alex Miller added a comment -

I don't think this enhancement makes sense as written - there are cases where it would be a breaking change for existing code.

I do think your specified problem makes sense though. One enhancement might be to have a variant of map-invert (different arity or map-invert-into that took an additional map target param).

Show
Alex Miller added a comment - I don't think this enhancement makes sense as written - there are cases where it would be a breaking change for existing code. I do think your specified problem makes sense though. One enhancement might be to have a variant of map-invert (different arity or map-invert-into that took an additional map target param).

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: