Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. (ns test.core
  2. (:import [clojure.lang IDeref]))
  3.  
  4. (require '[lazymap.core :as lm])
  5.  
  6. (defn lazy-assoc-in
  7. "Value should be either derefable (delay or future) which will be dereffed when value is needed
  8. or a function which will be called."
  9. [m [k & ks] f]
  10. (if (seq ks)
  11. (lm/delayed-assoc m k (delay (lazy-assoc-in (or (get m k) (lm/create-lazy-map {})) ks f)))
  12. (lm/delayed-assoc m k (if (instance? IDeref f)
  13. f
  14. (delay (f))))))
  15.  
  16. (def m (-> (lm/create-lazy-map {})
  17. (lazy-assoc-in [:a :b] (fn [] (do (println "lazy-get") 1)))
  18. (assoc-in [:a :c] 2)))
  19.  
  20. (-> m :a :c)
  21. ; => 2
  22.  
  23. (-> m :a :b)
  24. ; lazy-get
  25. ; => 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement