From 16eb27b342ec964b55a9d2351bfe1f4b38c2b0b0 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 28 Dec 2015 13:31:12 +0200 Subject: [PATCH] Add optimized version of index-by. --- frontend/uxbox/util/data.cljs | 38 ++--------------------------------- 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/frontend/uxbox/util/data.cljs b/frontend/uxbox/util/data.cljs index cf3424fb30..fb6443da53 100644 --- a/frontend/uxbox/util/data.cljs +++ b/frontend/uxbox/util/data.cljs @@ -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))