diff options
| author | Scott Ostler <scottbot9000@gmail.com> | 2011-02-13 20:06:24 -0500 |
|---|---|---|
| committer | Scott Ostler <scottbot9000@gmail.com> | 2011-02-13 20:06:24 -0500 |
| commit | f2f8cddb8b23fd3b932fccf1e40ced258ed71bb9 (patch) | |
| tree | 67ddc657c07cff7808ab5de7cea8a95b6eb31784 | |
| parent | d59f06da8e56c511dfd573cce2d8382dc043def1 (diff) | |
added sort-by-index-in, removed old index-of funcs
| -rw-r--r-- | src/tags.clj | 6 | ||||
| -rwxr-xr-x | src/utils.clj | 17 |
2 files changed, 13 insertions, 10 deletions
diff --git a/src/tags.clj b/src/tags.clj index fb530ea..76bc762 100644 --- a/src/tags.clj +++ b/src/tags.clj @@ -336,10 +336,8 @@ WHERE EXISTS (let [ids (map maybe-parse-int ids) query (fetch-dumps-by-message-id-query :num-messages (count ids)) raw-rows (do-select (vec (concat [query] ids))) - tagged-rows (map parse-tags-from-row-as-tag-map raw-rows) - index-func (fn [row] - (index-of #(= (:message_id row) %) ids))] - (for [m (sort-by index-func tagged-rows)] + tagged-rows (map parse-tags-from-row-as-tag-map raw-rows)] + (for [m (sort-by-index-in tagged-rows ids :message_id)] (let [favers (get (:tags m) "favorite") favorited (and viewer-nick (boolean (some #(= % viewer-nick) favers))) diff --git a/src/utils.clj b/src/utils.clj index 25814f9..84f185a 100755 --- a/src/utils.clj +++ b/src/utils.clj @@ -446,10 +446,15 @@ { :result result :time (System/currentTimeMillis)}) result))))) -;; From Programming Clojure by Stuart Halloway +;; Misc index/sorting funcs -(defn index-filter [pred coll] - (for [[idx elt] (indexed coll) :when (pred elt)] idx)) - -(defn index-of [pred coll] - (first (index-filter pred coll))) +(defn sort-by-index-in + ([c1 c2] (sort-by-index-in c1 c2 nil nil)) + ([c1 c2 f1] (sort-by-index-in c1 c2 f1 nil)) + ([c1 c2 f1 f2] + (let [c2-map (zipmap (if f2 (map f2 c2) c2) + (range (count c2))) + sort-func (if f1 + #(get c2-map (f1 %)) + #(get c2-map %))] + (sort-by sort-func c1)))) |
