mirror of
https://github.com/penpot/penpot.git
synced 2025-03-11 23:31:21 -05:00
Add optimized version of index-by.
This commit is contained in:
parent
adbc0c7edd
commit
16eb27b342
1 changed files with 2 additions and 36 deletions
|
@ -6,47 +6,13 @@
|
|||
;; Data structure manipulation
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
;; NOTE: commented because tansients are buggy in cljs?
|
||||
;; (defn index-by
|
||||
;; "Return a indexed map of the collection
|
||||
;; keyed by the result of executing the getter
|
||||
;; over each element of the collection."
|
||||
;; [coll getter]
|
||||
;; (let [data (transient {})]
|
||||
;; (run! #(do
|
||||
;; (println (getter %))
|
||||
;; (assoc! data (getter %) %)) coll)
|
||||
;; (println "test1:" (keys data))
|
||||
;; (let [r (persistent! data)]
|
||||
;; (println "test2:" (keys r))
|
||||
;; r)))
|
||||
|
||||
;; (defn index-by
|
||||
;; "Return a indexed map of the collection
|
||||
;; keyed by the result of executing the getter
|
||||
;; over each element of the collection."
|
||||
;; [coll getter]
|
||||
;; (let [data (transient {})]
|
||||
;; (loop [coll coll]
|
||||
;; (let [item (first coll)]
|
||||
;; (if item
|
||||
;; (do
|
||||
;; (assoc! data (getter item) item)
|
||||
;; (recur (rest coll)))
|
||||
;; (let [_ 1 #_(println "test1:" (keys data))
|
||||
;; r (persistent! data)]
|
||||
;; (println "test2:" (keys r))
|
||||
;; r))))))
|
||||
|
||||
(defn index-by
|
||||
"Return a indexed map of the collection
|
||||
keyed by the result of executing the getter
|
||||
over each element of the collection."
|
||||
[coll getter]
|
||||
(reduce (fn [acc item]
|
||||
(assoc acc (getter item) item))
|
||||
{}
|
||||
coll))
|
||||
(persistent!
|
||||
(reduce #(assoc! %1 (getter %2) %2) (transient {}) coll)))
|
||||
|
||||
(def ^:static index-by-id #(index-by % :id))
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue