Skip to content

Commit 9e06c07

Browse files
committed
re-frame: readability tweaks
1 parent 12c0a73 commit 9e06c07

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

frameworks/keyed/re-frame/src/demo/main.cljs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,36 +70,38 @@
7070
(assoc ::data {})
7171
(assoc ::data-ids []))))
7272

73+
(defn random-label []
74+
(str (rand-nth adjectives) " "
75+
(rand-nth colours) " "
76+
(rand-nth nouns)))
77+
7378
(rf/reg-event-db
7479
::append-data
7580
(fn [db [_ n]]
7681
(let [start (::max-id db 0)
7782
ids (range (inc start) (+ start n 1))
78-
new-data (apply merge
79-
(map (fn [id] {id {:id id
80-
:label (str (rand-nth adjectives) " " (rand-nth colours) " " (rand-nth nouns))}}) ids))]
83+
new-data
84+
(into {} (map #(into [%] {:id % :label (random-label)}) ids))]
8185
(-> db
82-
(assoc ::max-id (last ids))
86+
(assoc ::max-id (+ start n))
8387
(update ::data-ids into ids)
8488
(update ::data merge new-data)))))
8589

8690
(rf/reg-event-db
8791
::update-some
8892
(fn [db _]
89-
(let [ids (map-indexed (fn [idx id]
90-
(when (= 0 (mod idx 10)) id))
91-
(::data-ids db))
92-
new-data (apply merge
93-
(map (fn [id]
94-
{id {:id id
95-
:label (-> db
96-
::data
97-
(get id)
98-
:label
99-
(str " !!!"))}})
100-
ids))]
101-
(-> db
102-
(update ::data merge new-data)))))
93+
(let [updated-data
94+
(transduce
95+
(comp
96+
(map-indexed vector)
97+
(filter (fn [[idx _id]] (zero? (mod idx 10))))
98+
(map (fn [[_idx id]]
99+
[id (-> (get-in db [::data id])
100+
(update :label str " !!!"))])))
101+
merge
102+
{}
103+
(::data-ids db))]
104+
(update db ::data merge updated-data))))
103105

104106
(rf/reg-event-db
105107
::swap-rows

0 commit comments

Comments
 (0)