Skip to content

Instantly share code, notes, and snippets.

@msgodf
Created November 5, 2014 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msgodf/b31203dfc9d6cd7b16ec to your computer and use it in GitHub Desktop.
Save msgodf/b31203dfc9d6cd7b16ec to your computer and use it in GitHub Desktop.
A function to update multiple parts of a structure.given a function that gives the locations of those parts.
(defn update-at
"Takes a structure, a function that returns a list of paths (lists of keys) and a
function to apply at those paths within the structure.
For any paths that don't already exist in m, then f will be called with nil
and that path in the structure will be created with the result."
[m path-generation-fn f]
(reduce (fn [s v] (update-in s v f))
m
(path-generation-fn m)))
@msgodf
Copy link
Author

msgodf commented Nov 5, 2014

Perhaps this should just take a list of paths and leave the calling function to call the path generating function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment