Parcourir la Source

Change transplant to allow for an arbitrary eq-relation.

Lucas Stadler 13 ans auparavant
Parent
commit
2bb72b05e2
1 fichiers modifiés avec 18 ajouts et 10 suppressions
  1. 18 10
      clj/shame.clj

+ 18 - 10
clj/shame.clj

@ -18,6 +18,22 @@
18 18
19 19
;; todo manipulation
20 20
21
(defn get-by [map-coll k v]
22
  (first (drop-while #(not= (k %) v) map-coll)))
23
24
(defn eq-by-key [key m1 m2]
25
  (= (key m1) (key m2)))
26
27
(defn same-name [m1 m2]
28
  (eq-by-key :name m1 m2))
29
30
(defn transplant [item association from to & [eql-rel]]
31
  "Transplants an item in a map of vectors from one key to another."
32
  (let [eql-rel (or eql-rel =)])
33
  (assoc association
34
         from (vec (filter #(not ((or eql-rel =) item %)) (from association)))
35
         to   (conj (to association) item)))
36
21 37
(defn add-item [item shaming]
22 38
  "Try adding an item to the shaming. If the maximum number of items is
23 39
   exceeeded, return the original shaming."
@ -26,24 +42,16 @@
26 42
      (assoc-in shaming [:current c] item)
27 43
      shaming))) ; FIXME: is that the clojure way of doing it?
28 44
29
(defn get-by [map-coll k v]
30
  (first (drop-while #(not= (k %) v) map-coll)))
31
32
(defn transplant [item association from to]
33
  "Transplants an item in a map of vectors from one key to another."
34
  (assoc association
35
         from (vec (filter #(not= item %) (from association)))
36
         to   (conj (to association) item)))
37 45
38 46
(defn close-item [item-name status shaming]
39 47
  (let [item (get-by (:current shaming) :name item-name)
40 48
        item (assoc item :closed-at (java.util.Date.))]
41
    (transplant item shaming :current :past)))
49
    (transplant item shaming :current :past same-name)))
42 50
43 51
(defn resurrect-item [item-name shaming]
44 52
  (let [item (get-by (:past shaming) :name item-name)
45 53
        item (assoc item :started-at (java.util.Date.))]
46
    (transplant item shaming :past :current)))
54
    (transplant item shaming :past :current same-name)))
47 55
48 56
;; references (for "mutating" the todo-list)
49 57