-
-
Save Deraen/ae4adfd50df6010b1f2a to your computer and use it in GitHub Desktop.
lazy-assoc-in
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
(ns test.core | |
(:import [clojure.lang IDeref])) | |
(require '[lazymap.core :as lm]) | |
(defn lazy-assoc-in | |
"Value should be either derefable (delay or future) which will be dereffed when value is needed | |
or a function which will be called." | |
[m [k & ks] f] | |
(if (seq ks) | |
(lm/delayed-assoc m k (delay (lazy-assoc-in (or (get m k) (lm/create-lazy-map {})) ks f))) | |
(lm/delayed-assoc m k (if (instance? IDeref f) | |
f | |
(delay (f)))))) | |
(def m (-> (lm/create-lazy-map {}) | |
(lazy-assoc-in [:a :b] (fn [] (do (println "lazy-get") 1))) | |
(assoc-in [:a :c] 2))) | |
(-> m :a :c) | |
; => 2 | |
(-> m :a :b) | |
; lazy-get | |
; => 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment