Skip to content

Instantly share code, notes, and snippets.

@fogus
Forked from alandipert/paths.clj
Last active August 29, 2015 14:14
Show Gist options
  • Save fogus/3247273357598a3c259e to your computer and use it in GitHub Desktop.
Save fogus/3247273357598a3c259e to your computer and use it in GitHub Desktop.
(defn paths
([root] (paths [] root))
([parent x]
(if (map? x)
(mapcat (fn [[k v]] (paths (conj parent k) v)) x)
[parent])))
(def m
{:q {:z 1}
:x {:y {:a 1}
:b {:b 2}}})
(paths m) ;=> ([:q :z] [:x :y :a] [:x :b :b])
(reduce #(update-in %1 %2 dec) m (paths m))
;=> {:q {:z 0}, :x {:y {:a 0}, :b {:b 1}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment