From b77115850fa485a2e7e56f065e97f03f8f0cb50a Mon Sep 17 00:00:00 2001 From: Vincent Cantin Date: Mon, 21 Jan 2019 23:18:19 +0800 Subject: [PATCH 1/2] Rewrote `reitit.core/path-conflicting-routes` using transducers. --- modules/reitit-core/src/reitit/core.cljc | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/reitit-core/src/reitit/core.cljc b/modules/reitit-core/src/reitit/core.cljc index a2f090d0..e88b15bc 100644 --- a/modules/reitit-core/src/reitit/core.cljc +++ b/modules/reitit-core/src/reitit/core.cljc @@ -65,17 +65,17 @@ (cond->> (->> (walk raw-routes opts) (map-data merge-data)) coerce (into [] (keep #(coerce % opts))))) -;; This whole function might be more efficient and easier to understand with transducers. (defn path-conflicting-routes [routes] - (some->> - (loop [[r & rest] routes, acc {}] - (if (seq rest) - (let [conflicting (set (keep #(if (impl/conflicting-routes? r %) %) rest))] - (recur rest (update acc r (fnil (comp set concat) #{}) conflicting))) - acc)) - (filter (comp seq second)) - (seq) - (into {}))) + (let [conflicting-routes + (into {} + (comp (map-indexed (fn [index route] + [route (into #{} + (filter #(impl/conflicting-routes? route %)) + (subvec routes (inc index)))])) + (filter (comp seq second))) + routes)] + (when (seq conflicting-routes) + conflicting-routes))) (defn conflicting-paths [conflicts] (->> (for [[p pc] conflicts] From fdf249a959981c916d7f53a624c2214ed4fa4583 Mon Sep 17 00:00:00 2001 From: Vincent Cantin Date: Tue, 22 Jan 2019 08:22:48 +0800 Subject: [PATCH 2/2] Apply a change suggested by Miikka Koskinen. --- modules/reitit-core/src/reitit/core.cljc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/modules/reitit-core/src/reitit/core.cljc b/modules/reitit-core/src/reitit/core.cljc index e88b15bc..b905915b 100644 --- a/modules/reitit-core/src/reitit/core.cljc +++ b/modules/reitit-core/src/reitit/core.cljc @@ -66,16 +66,14 @@ coerce (into [] (keep #(coerce % opts))))) (defn path-conflicting-routes [routes] - (let [conflicting-routes - (into {} - (comp (map-indexed (fn [index route] - [route (into #{} - (filter #(impl/conflicting-routes? route %)) - (subvec routes (inc index)))])) - (filter (comp seq second))) - routes)] - (when (seq conflicting-routes) - conflicting-routes))) + (-> (into {} + (comp (map-indexed (fn [index route] + [route (into #{} + (filter #(impl/conflicting-routes? route %)) + (subvec routes (inc index)))])) + (filter (comp seq second))) + routes) + (not-empty))) (defn conflicting-paths [conflicts] (->> (for [[p pc] conflicts]