From a1502fbbba0445c0f7b659c391e7355dcb62a74c Mon Sep 17 00:00:00 2001 From: Erwin Rooijakkers Date: Wed, 20 Jan 2021 22:28:21 +0100 Subject: [PATCH 1/3] Align and do not use default with apply min Since (apply min 0) is always 0 --- .../src/packt-clj/game_values.clj | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/Chapter04/Activity4.01/src/packt-clj/game_values.clj b/Chapter04/Activity4.01/src/packt-clj/game_values.clj index 9ce19bc..7ba40ff 100644 --- a/Chapter04/Activity4.01/src/packt-clj/game_values.clj +++ b/Chapter04/Activity4.01/src/packt-clj/game_values.clj @@ -1,6 +1,5 @@ (ns packt-clj.game-values) - (def game-users [{:id 9342 :username "speedy" @@ -63,22 +62,19 @@ :experience-level 8 :status :active}]) - (defn max-value-by-status [field status users] - (->> - users - ;; step 1: use filter to only keep users who - ;; have the status we are looking for - (filter #(= (:status %) status)) - ;; step 2: field is a keyword, so we can use it as - ;; a function when calling map. - (map field) - ;; step 3: use the apply max pattern, with a default - (apply max 0))) + (->> users + ;; step 1: use filter to only keep users who + ;; have the status we are looking for + (filter #(= (:status %) status)) + ;; step 2: field is a keyword, so we can use it as + ;; a function when calling map. + (map field) + ;; step 3: use the apply max pattern, with a default + (apply max 0))) (defn min-value-by-status [field status users] - (->> - users - (filter #(= (:status %) status)) - (map field) - (apply min 0))) + (->> users + (filter #(= (:status %) status)) + (map field) + (apply min))) ;; do not use a default, (min '()) returns nil From 42d3106f898d534243567ed47d2d03af032de071 Mon Sep 17 00:00:00 2001 From: Erwin Rooijakkers Date: Wed, 20 Jan 2021 23:23:01 +0100 Subject: [PATCH 2/3] Fix direct routes There's a bug in the code for direct routes that are not over multiple hops. min-route expects a collection so we should always return a collection. This way it works for both direct and indirect routes: (find-path (grouped-routes routes) :paris :london) ;; => {:cost 236, :best [:paris :london]} (find-path (grouped-routes routes) :paris :budapest) ;; => {:cost 251, :best [:paris :milan :vienna :budapest]} --- Chapter06/Exercise6.07/train_routes.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter06/Exercise6.07/train_routes.clj b/Chapter06/Exercise6.07/train_routes.clj index 326c949..45e6776 100644 --- a/Chapter06/Exercise6.07/train_routes.clj +++ b/Chapter06/Exercise6.07/train_routes.clj @@ -55,7 +55,7 @@ (= position destination) path (get-in route-lookup [position destination]) - (conj path destination) + [(conj path destination)] :otherwise-we-search (let [path-set (set path) From f7ac86fcaf8077a69b1d853428de348fe2e88fce Mon Sep 17 00:00:00 2001 From: Erwin Rooijakkers Date: Wed, 12 Jul 2023 22:26:44 +0200 Subject: [PATCH 3/3] Remove incorrect comment --- Chapter04/Activity4.01/src/packt-clj/game_values.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter04/Activity4.01/src/packt-clj/game_values.clj b/Chapter04/Activity4.01/src/packt-clj/game_values.clj index 7ba40ff..3dfcf6b 100644 --- a/Chapter04/Activity4.01/src/packt-clj/game_values.clj +++ b/Chapter04/Activity4.01/src/packt-clj/game_values.clj @@ -77,4 +77,4 @@ (->> users (filter #(= (:status %) status)) (map field) - (apply min))) ;; do not use a default, (min '()) returns nil + (apply min)))