Created
December 27, 2013 16:02
-
-
Save cjfrisz/8149003 to your computer and use it in GitHub Desktop.
Slight augment to Clojure's update-in function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; the following function works much like clojure.core/update-in, but | |
;; also works in the case that the nested data structure being traversed | |
;; includes a list from which you want to get the first element. | |
;; it's woefully incapable of handling anything more complex, as the | |
;; aforementioned use case was the only one I had when I wrote it. | |
(defn update-in+ | |
"Similar to clojure.core/update-in, except coll may include lists and | |
key* may include references to clojure.core/first so that lists in coll | |
can be traversed." | |
[coll key* f & arg*] | |
(letfn [($update-in+ [coll key*] | |
(if-let [key (first key*)] | |
(let [key* (next key*)] | |
(if (identical? key clojure.core/first) | |
(conj (next coll) ($update-in+ (first coll) key*)) | |
(assoc coll key ($update-in+ (get coll key) key*)))) | |
(apply f coll arg*)))] | |
($update-in+ coll key*))) | |
;; used for early testing in clojure.tools.cps to only apply the CPS | |
;; transform to the first overload of a fn expression. | |
;; since the CPS transformer can support multi-overload fn expressions, | |
;; this is no longer used for clojure.tools.cps, but it seemed like it | |
;; should be saved for posterity. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment