Clojure

update-in with empty key paths

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Patch:
    Code and Test
  • Approval:
    Triaged

Description

To the topic of get-in and update-in. While I realize this is not a bug it is odd and in my eyes unexpected and unwanted behavior.

get-in called with an empty path returns the hashmap it was given to walk through - this is as I expect and makes a lot of sense when dynamically (with generated pathes ) walking a hash map.

update-in behaves differently and while from the implementation side, it's behavior too makes sense, it does not work as expected (at least not for me) update-in with an empty map creates a new key 'nil' so:

(update-in {...} [] f) ist he same as (update-in {...} [nil] f) while (get-in {...} []) is not the same as (get-in {...} [nil]) and of cause differs from what update-in does.

For automatically walking trees the behavior of get-in makes a lot more sense since the current behavior of update-in forces you to check for empty paths and if they are empty fall back to plain assoc and get (or get-in since this works):

(if-let [r (butlast @path)]
(do
  (alter m update-in r dissoc (last @path))
  (alter m update-in r assoc {:name @sr} c))
(do
  (alter m dissoc (last @path))
  (alter m assoc {:name @sr} c)))

Next argument is that update-in with an empty map working on nil isn't easy to gasp, one needs to know the implementation details to realize that it works, I think 90% of the people reading update-in with [] will not instinctively know that it works on the key nil, so changing this would most likely not break any current code, and if it would the code would be bad anyway .

Chouser has, a very nice solution on the mailing list that would fix the problem I'm not sure if I'm entitled to post it here since I did not wrote it but it can be found in this thread: http://groups.google.com/group/clojure/browse_thread/thread/de5b20b8c3fe498b?hl=en

Regards,
Heinz

Activity

Rich Hickey made changes -
Field Original Value New Value
Issue Type Defect [ 1 ] Enhancement [ 4 ]
Reporter Assembla Importer [ importer ]
Priority Blocker [ 1 ]
Chouser made changes -
Attachment 0001-Support-empty-path-in-update-in.-CLJ-373.patch [ 10019 ]
Approval Vetted Test
Stuart Halloway made changes -
Priority Blocker [ 1 ] Minor [ 4 ]
Approval Test Incomplete
Waiting On chouser@n01se.net
Alexander Redington made changes -
Fix Version/s Approved Backlog [ 10034 ]
Fix Version/s Release.Next [ 10038 ]
Scott Lowe made changes -
Andy Fingerhut made changes -
Patch Code and Test [ 10002 ]
Andy Fingerhut made changes -
Rich Hickey made changes -
Approval Incomplete [ 10006 ] Vetted [ 10003 ]
Fix Version/s Approved Backlog [ 10034 ]
Fix Version/s Release 1.5 [ 10150 ]
Fogus made changes -
Assignee Fogus [ fogus ]
Fogus made changes -
Approval Vetted [ 10003 ] Incomplete [ 10006 ]
Rich Hickey made changes -
Fix Version/s Release 1.5 [ 10150 ]
Fogus made changes -
Assignee Fogus [ fogus ]
Alex Miller made changes -
Approval Incomplete [ 10006 ]
Ambrose Bonnaire-Sergeant made changes -
Attachment CLJ-373-nested-ops.patch [ 12595 ]
Alex Miller made changes -
Approval Triaged [ 10120 ]
Ambrose Bonnaire-Sergeant made changes -
Attachment CLJ-373-nested-ops.patch [ 12595 ]
Ambrose Bonnaire-Sergeant made changes -
Attachment CLJ-373-nested-ops.patch [ 12897 ]

People

Vote (5)
Watch (6)

Dates

  • Created:
    Updated: