Permalink
Browse files

Eval record expr only once

  • Loading branch information...
1 parent a9cd7d9 commit 5ee535405c18445ff0bc68fae7d756166be0c768 @amalloy amalloy committed Nov 3, 2011
Showing with 10 additions and 6 deletions.
  1. +10 −6 src/useful/datatypes.clj
View
@@ -49,24 +49,27 @@
"Assoc attrs into a record. Mapping fields into constuctor arguments is done at compile time,
so this is more efficient than calling assoc on an existing record."
[record & attrs]
- (let [type (type-hint record &env 'assoc-record)
+ (let [r (gensym 'record)
+ type (type-hint record &env 'assoc-record)
fields (record-fields type)
index (position fields)
vals (reduce (fn [vals [field val]]
(if-let [i (index (normalize-field-name field))]
(assoc vals i val)
(assoc-in vals
[(index '--extmap) (keyword field)] val)))
- (vec (map #(list (symbol (str "." %)) record) fields))
+ (vec (map #(list '. r %) fields))
(into-map attrs))]
- `(new ~type ~@vals)))
+ `(let [~r ~record]
+ (new ~type ~@vals))))
(defmacro update-record
"Construct a record given a list of forms like (update-fn record-field & args). Mapping fields
into constuctor arguments is done at compile time, so this is more efficient than calling assoc on
an existing record."
[record & forms]
- (let [type (type-hint record &env 'update-record)
+ (let [r (gensym 'record)
+ type (type-hint record &env 'update-record)
fields (record-fields type)
index (position fields)
vals (reduce (fn [vals [f field & args]]
@@ -76,9 +79,10 @@
(let [i (index '--extmap)]
(assoc vals
i (apply list `update (get vals i) (keyword field) args)))))
- (vec (map #(list (symbol (str "." %)) record) fields))
+ (vec (map #(list '. r %) fields))
forms)]
- `(new ~type ~@vals)))
+ `(let [~r ~record]
+ (new ~type ~@vals))))
(defmacro record-accessors
"Defines optimized macro accessors using interop and typehints for all fields in the given records."

0 comments on commit 5ee5354

Please sign in to comment.