Persistent map with lazily evaluated values for Clojure(Script).
zizz
(noun) A nap.
DEPRECATED. Use https://github.com/strojure/zmap with more explicit API and practical naming.
- Access map values with delayed evaluation as ordinary map values.
- Pass maps with delayed values to code which should not care that values are delayed.
- Keep pending delayed values not realized until used.
- Do not mix zizzmap-delays with delays created by
clojure.core/delay
. - Support
IFn
interface of persistent map. - Support transients.
- Transparent IPersistentMap behaviour with minimal overhead.
The zizzmap
can be used when large map need to be created but most of the map
keys will never be used. For example ring request map with multiple conversions
from HTTP request to Clojure data structure.
The macro init
is used to define map with delayed values.
(ns project.readme.core-01-init
(:require [strojure.zizzmap.core :as zizz]))
(def ^:private -map
(zizz/init {:a (do (println "Init") 1)}))
(get -map :a)
;Init
;=> 1
The macro assoc*
returns map with delayed value at specified key.
(def ^:private -map1
(zizz/assoc* {} :a (do (println "Init") 1)))
(get -map1 :a)
;Init
;=> 1
(def ^:private -map2
(zizz/assoc* {}
:a (do (println "Init :a") 1)
:b (do (println "Init :b") 2)))
(get -map2 :b)
;Init
;=> 2
The function merge*
is like clojure.core/merge
but keeps pending values not
realized.
(ns project.readme.core-03-merge
(:require [strojure.zizzmap.core :as zizz]))
(def ^:private -merged
(zizz/merge* {:a 1}
(zizz/init {:b (do (println "Init") 2)})))
(get -merged :a)
;=> 1
(get -merged :b)
;Init
;=> 2
The function update*
is like clojure.core/update
but delays application of
function f
to map value.
(ns project.readme.core-04-update
(:require [strojure.zizzmap.core :as zizz]))
;;; Update delayed value
(def ^:private -map1
(-> (zizz/init {:a (do (println "Init") 1)})
(zizz/update* :a inc)))
(get -map1 :a)
;Init
;=> 2
;;; Delayed update in standard map
(def ^:private -map2
(-> {:a 1}
(zizz/update* :a (fn [a]
(println "Update")
(inc a)))))
(get -map2 :a)
;Update
;=> 2
The map can be constructed manually using delay*
and convert-map
.
(ns project.readme.core-05-delay
(:require [strojure.zizzmap.core :as zizz]))
(def ^:private -map
(-> {:a (zizz/delay* (println "Init")
1)}
(zizz/convert-map)))
(get -map :a)
;Init
;=> 1
See some benchmarks here.