Commits

Meikel Brandmeyer committed e2357a0

Add future-* variants

Comments (0)

Files changed (1)

src/main/clojure/lazymap/core.clj

 
 (defprotocol ILazyPersistentMap
   "ILazyPersistentMap extends IPersistentMap with a method to allow
-  transportation of the underlying delay objects."
-  (delay-assoc [lazy-map key delay] "Associates the given delay in the map."))
+  transportation of the underlying delaying objects."
+  (delay-assoc [lazy-map key delay] "Associates the given delay/future in the map."))
 
 (deftype LazyMapEntry [k v]
   ILazyMapEntry
    (LazyPersistentMap. base metadata)))
 
 (defn- quote-values
-  [kvs]
+  [kvs ctor]
   (assert (even? (count kvs)))
-  (mapcat (fn [[k v]] [k `(delay ~v)]) (partition 2 kvs)))
+  (mapcat (fn [[k v]] [k `(~ctor ~v)]) (partition 2 kvs)))
+
+(defn delayed-assoc
+  "delayed-assoc is like lazy-assoc but a function and takes values as delays
+  or futures instead of expanding into a delay/future of val."
+  [m & kvs]
+  (assert (even? (count kvs)))
+  (reduce (fn [m [k v]] (delay-assoc m k v)) m (partition 2 kvs)))
+
+(defn delayed-hash-map
+  "delayed-hash-map is the same as lazy-hash-map except that its a function
+  and it takes a seq of keys-delayed-value pairs."
+  [& kvs]
+  (create-lazy-map (apply hash-map kvs)))
+
+(defn delayed-sorted-map
+  "delayed-sorted-map is the same as lazy-sorted-map except that its a
+  function and it takes a seq of keys-delayed-value pairs."
+  [& kvs]
+  (create-lazy-map (apply sorted-map kvs)))
+
+(defn delayed-struct-map
+  "delayed-struct-map is the same as lazy-struct-map except that its a
+  function and it takes a seq of keys-delayed-value pairs together with the
+  struct basis."
+  [s & kvs]
+  (create-lazy-map (apply struct-map s kvs)))
+
+(defn delayed-struct
+  "delayed-struct is the same as lazy-struct except that its a function and
+  it takes a seq of delayed value together with the struct basis."
+  [s & vs]
+  (create-lazy-map (apply struct s vs)))
 
 (defn lazy-assoc*
   "lazy-assoc* is like lazy-assoc but a function and takes values as delays
-  instead of expanding into a delay of val."
+  instead of expanding into a delay of val.
+
+  Deprecated: Use delayed-assoc instead."
+  {:deprecated true}
   [m & kvs]
-  (assert (even? (count kvs)))
-  (reduce (fn [m [k v]] (delay-assoc m k v)) m (partition 2 kvs)))
+  (apply delayed-assoc m kvs))
 
 (defmacro lazy-assoc
   "lazy-assoc associates new values to the given keys in the given lazy map.
   The values are not evaluated, before their first retrieval. They are
   evaluated at most once."
   [m & kvs]
-  `(lazy-assoc* ~m ~@(quote-values kvs)))
+  `(delayed-assoc ~m ~@(quote-values kvs `delay)))
 
 (defn lazy-hash-map*
   "lazy-hash-map* is the same as lazy-hash-map except that its a function
-  and it takes a seq of keys-delayed-value pairs."
+  and it takes a seq of keys-delayed-value pairs.
+
+  Deprecated: Use delayed-hash-map."
+  {:deprecated true}
   [& kvs]
-  (create-lazy-map (apply hash-map kvs)))
+  (apply delayed-hash-map kvs))
 
 (defmacro lazy-hash-map
   "lazy-hash-map creates a map. The values are not evaluated before their
   first retrieval. Each value is evaluated at most once. The underlying map
   is a hash map."
   [& kvs]
-  `(lazy-hash-map* ~@(quote-values kvs)))
+  `(delayed-hash-map ~@(quote-values kvs `delay)))
 
 (defn lazy-sorted-map*
   "lazy-sorted-map* is the same as lazy-sorted-map except that its a
-  function and it takes a seq of keys-delayed-value pairs."
+  function and it takes a seq of keys-delayed-value pairs.
+
+  Deprecated: Use delayed-sorted-map"
+  {:deprecated true}
   [& kvs]
-  (create-lazy-map (apply sorted-map kvs)))
+  (apply delayed-sorted-map kvs))
 
 (defmacro lazy-sorted-map
   "lazy-sorted-map creates a map. The values are not evaluated before their
   first retrieval. Each value is evaluated at most once. The underlying map
   is a sorted map."
   [& kvs]
-  `(lazy-sorted-map* ~@(quote-values kvs)))
+  `(delayed-sorted-map ~@(quote-values kvs `delay)))
 
 (defn lazy-struct-map*
   "lazy-struct-map* is the same as lazy-struct-map except that its a
   function and it takes a seq of keys-delayed-value pairs together with the
-  struct basis."
+  struct basis.
+
+  Deprecated: Use delayed-struct-map"
+  {:deprecated true}
   [s & kvs]
-  (create-lazy-map (apply struct-map s kvs)))
+  (apply delayed-struct-map s kvs))
 
 (defmacro lazy-struct-map
   "lazy-struct-map creates a map. The values are not evaluated before their
   first retrieval. Each value is evaluated at most once. The underlying map
   is a struct map according to the provided structure s."
   [s & kvs]
-  `(lazy-struct-map* ~s ~@(quote-values kvs)))
+  `(delayed-struct-map ~s ~@(quote-values kvs `delay)))
 
 (defn lazy-struct*
   "lazy-struct* is the same as lazy-struct except that its a function and
-  it takes a seq of delayed value together with the struct basis."
+  it takes a seq of delayed value together with the struct basis.
+
+  Deprecated: Use delayed-struct"
+  {:deprecated true}
   [s & vs]
-  (create-lazy-map (apply struct s vs)))
+  (apply delayed-struct s vs))
 
 (defmacro lazy-struct
   "lazy-struct creates a map. The values are not evaluated before their
   struct the values have to appear in the order of the keys in the structure."
   [s & vs]
   (let [vs (map (fn [v] `(delay ~v)) vs)]
-    `(lazy-struct* ~s ~@vs)))
+    `(delayed-struct ~s ~@vs)))
+
+(defmacro future-assoc
+  "future-assoc associates new values to the given keys in the given lazy map.
+  The values are evaluated in a separate thread. They are evaluated at most
+  once."
+  [m & kvs]
+  `(delayed-assoc ~m ~@(quote-values kvs `future)))
+
+(defmacro future-hash-map
+  "future-hash-map creates a map. The values are evaluated in a separate
+  thread. Each value is evaluated at most once. The underlying map is a
+  hash map."
+  [& kvs]
+  `(delayed-hash-map ~@(quote-values kvs `future)))
+
+(defmacro future-sorted-map
+  "future-hash-map creates a map. The values are evaluated in a separate
+  thread. Each value is evaluated at most once. The underlying map is a
+  sorted map."
+  [& kvs]
+  `(delayed-sorted-map ~@(quote-values kvs `future)))
+
+(defmacro future-struct-map
+  "future-hash-map creates a map. The values are evaluated in a separate
+  thread. Each value is evaluated at most once. The underlying map is a
+  struct map according to the provided structure s."
+  [s & kvs]
+  `(delayed-struct-map ~s ~@(quote-values kvs `future)))
+
+(defmacro future-struct
+  "future-hash-map creates a map. The values are evaluated in a separate
+  thread. Each value is evaluated at most once. The underlying map is a
+  struct map according to the provided structure s. As with Clojure's
+  struct the values have to appear in the order of the keys in the structure."
+  [s & vs]
+  (let [vs (map (fn [v] `(future ~v)) vs)]
+    `(delayed-struct ~s ~@vs)))