Skip to content

Instantly share code, notes, and snippets.

@ikitommi
Created October 7, 2015 05:53
Show Gist options
  • Save ikitommi/9b83cb75ad3b3e054a46 to your computer and use it in GitHub Desktop.
Save ikitommi/9b83cb75ad3b3e054a46 to your computer and use it in GitHub Desktop.
lazy-assoc-in

Need to hava a lazy assoc-in operation. As lazy-assoc is a macro, doesn't compose by default.

lein repl (sample using Vinyasa pull):

(./pull 'de.kotka/lazymap "3.1.1")
; {[de.kotka/lazymap "3.1.1"] #{[org.clojure/clojure "1.2.0"]}
;  [org.clojure/clojure "1.2.0"] nil}

(require '[lazymap.core :as lm])

;; FIXME, use ks to really do the update
(defmacro lazy-assoc-in [m ks v]
  `(lm/lazy-assoc
     ~m
     :a (lm/lazy-assoc
          (lm/create-lazy-map {})
          :b ~v)))

(def m (-> (lm/create-lazy-map {})
           (lazy-assoc-in [:a :b] (do (println "lazy-get") 1))
           (assoc-in [:a :c] 2)))

(-> m :a :c)
; => 1

(-> m :a :b)
; lazy-get
; => 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment