Skip to content

Instantly share code, notes, and snippets.

@friemen
Created July 14, 2015 18:45
Show Gist options
  • Save friemen/b2daf61c3d18a8d76184 to your computer and use it in GitHub Desktop.
Save friemen/b2daf61c3d18a8d76184 to your computer and use it in GitHub Desktop.
Multi-path update
user> (def data {:x 110 ::old {:x 100}})
#'user/data
user> (update-in data [::old] merge {:x 110 :z 5})
{:user/old {:z 5, :x 110}, :x 110}
user> (-> data
(update-in [::old] merge {:x 110 :z 5})
(merge {:x 200}))
{:user/old {:z 5, :x 110}, :x 200}
user> (defn update-keep-old
[m k v]
(let [ov (m k)]
(-> m
(assoc k v)
(update-in [::old] k ov))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment