Bladeren bron

Change transplant to allow for an arbitrary eq-relation.

Lucas Stadler 13 jaren geleden
bovenliggende
commit
2bb72b05e2
1 gewijzigde bestanden met toevoegingen van 18 en 10 verwijderingen
  1. 18 10
      clj/shame.clj

+ 18 - 10
clj/shame.clj

18
18
19
;; todo manipulation
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
(defn add-item [item shaming]
37
(defn add-item [item shaming]
22
  "Try adding an item to the shaming. If the maximum number of items is
38
  "Try adding an item to the shaming. If the maximum number of items is
23
   exceeeded, return the original shaming."
39
   exceeeded, return the original shaming."
26
      (assoc-in shaming [:current c] item)
42
      (assoc-in shaming [:current c] item)
27
      shaming))) ; FIXME: is that the clojure way of doing it?
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
(defn close-item [item-name status shaming]
46
(defn close-item [item-name status shaming]
39
  (let [item (get-by (:current shaming) :name item-name)
47
  (let [item (get-by (:current shaming) :name item-name)
40
        item (assoc item :closed-at (java.util.Date.))]
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
(defn resurrect-item [item-name shaming]
51
(defn resurrect-item [item-name shaming]
44
  (let [item (get-by (:past shaming) :name item-name)
52
  (let [item (get-by (:past shaming) :name item-name)
45
        item (assoc item :started-at (java.util.Date.))]
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
;; references (for "mutating" the todo-list)
56
;; references (for "mutating" the todo-list)
49
57