lazy-map - lazy maps for Clojure

Description

With lazy-cons you can create a lazy form of a list, which doesn't evaluate it's tail until you actually access it. For maps there is no such equivalent. This package tries to address this.

It defines lazy-hash-map, lazy-sorted-map and lazy-struct-map constructors which create a new map of the given type, whose values are not evaluated. As soon as they are accessed for the first time, they are evaluated and their value is memoized. So the values are evaluated at most once.

With lazy-assoc you can associate a new value to a lazy map. Again the value is not evaluated until accessed. In case the key is already present in the map, its value is replaced with the new one.

The map itself is a function of its keys. So to retrieve a key, one simply „calls“ the map and supplies the key as argument.

Lazy maps also support meta data. So they are a complete drop-in where any map would work.

Acknowledgement

A big „thank you“ goes to Chouser for his inputs and help in the clojure group and on the #clojure channel. Lazy map came from closures, went over proxies to a short Java intermezzo and finally ended up with gen-class. It was a long way.

Issues

Example

A short example on the usage:


(def the-map (lazy-hash-map :TheKey (do (println "Evaluated!") "The Value")))
(println "Not evaluated!")
(println (the-map :TheKey))

This will result in the following output:

Not evaluated!
Evaluated!
The Value

Download

Current Version Source:

Current Version JAR:

Development Version: