Skip to content

Commit 59f5c0d

Browse files
committed
Refactoring on sorted agents function for composition
1 parent 748bc84 commit 59f5c0d

File tree

1 file changed

+14
-26
lines changed
  • programming_challenges/playground/jobs

1 file changed

+14
-26
lines changed

programming_challenges/playground/jobs/job.clj

+14-26
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
(some #(= (:type job) %) (:secondary_skillset agent))))
3535

3636
(defn filter-by-skillset
37-
[current-job agents]
38-
(filter #(by-skillset current-job %) agents))
37+
[job agents]
38+
(filter #(by-skillset job %) agents))
3939
;; -- end of filtering by skillsets --
4040

4141
;; -- examples of function use --
@@ -45,41 +45,29 @@
4545
;; -- end of examples of function use --
4646

4747
;; -- sorting by job type --
48-
(defn agent-has-job-type-as-skillset?
48+
(defn agent-has-job-type-as-primary-skillset?
4949
[job-type agent]
5050
(if (some #{job-type} (:primary_skillset agent))
5151
0
5252
1))
5353

54-
(defn has-skillset-and-id-pair
54+
(defn add-has-primary-skillset-key
5555
[job-type agent]
56-
[(agent-has-job-type-as-skillset? job-type agent)
57-
(:id agent)])
56+
(assoc
57+
agent
58+
:has-primary-skillset
59+
(agent-has-job-type-as-primary-skillset? job-type agent)))
5860

59-
(defn find-agent-by-id
60-
[id agents]
61-
(->> agents
62-
(filter (fn [agent] (= id (:id agent))))
63-
(first)))
64-
65-
(defn build-agent-by-id
66-
[agent-id agents]
67-
{:id agent-id
68-
:name (:name (find-agent-by-id agent-id agents))
69-
:primary_skillset (:primary_skillset (find-agent-by-id agent-id agents))
70-
:secondary_skillset (:secondary_skillset (find-agent-by-id agent-id agents))})
71-
72-
(defn rebuild-agent
73-
[agent agents]
74-
(let [agent-id (second agent)]
75-
(build-agent-by-id agent-id agents)))
61+
(defn remove-has-primary-skillset-key
62+
[agent]
63+
(dissoc agent :has-primary-skillset))
7664

7765
(defn sorted-agents
7866
[agents job]
7967
(->> agents
80-
(map (partial has-skillset-and-id-pair (:type job)))
81-
(sort-by first)
82-
(map #(rebuild-agent % agents))))
68+
(map (partial add-has-primary-skillset-key (:type job)))
69+
(sort-by :has-primary-skillset)
70+
(map remove-has-primary-skillset-key)))
8371

8472
;; ----- Testing sorted agents -----
8573
(defn testing []

0 commit comments

Comments
 (0)