Skip to content

Instantly share code, notes, and snippets.

@Deraen
Forked from ikitommi/repl.md
Created October 7, 2015 06:30
Show Gist options
  • Save Deraen/ae4adfd50df6010b1f2a to your computer and use it in GitHub Desktop.
Save Deraen/ae4adfd50df6010b1f2a to your computer and use it in GitHub Desktop.
lazy-assoc-in
(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