Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement