specter

Latest version: 1.1.3
2021-03-30T18:45:02.003900Z

Hey team, noob question (not quite sure how to search for above) I want to do the following:

(def xs [{:name "foo" :x 1}]
(def to-add-a {:name "foo" :x nil})
(def to-add-b {:name "bar" :x nil})
I want to updated xs, so a) If the record’s name matches, we do nothing:
xs + to-add-a => [{:name "foo" :x 1}]
b) but if the record does not exist, to append it:
xs + to add-b => [{:name "foo"  :x 1} {:name "bar" :x nil}]
How could I achieve this with spectr?

nathanmarz 2021-03-30T18:54:35.004400Z

@stopachka this would be one way to do it:

(defn add-record [all record]
  (setval [(not-selected? ALL :name (pred= (:name record)))
           AFTER-ELEM]
           record
           all))

2021-03-30T18:55:11.004600Z

niice. Thanks @nathanmarz!

2021-03-30T20:08:10.007100Z

Okay, one more: Say I have

(def name->objects {"foo": [{:name "foo" :x 1]})
(def to-add-a {:name "foo" :x nil})
(def to-add-b {:name "bar" :x nil})
Now, I want to: a. Either append into existing:
name->objects + to-add-a => {"foo" [{:name "foo" :x 1] {:name "foo" :x nil}]}
b. Or create the key and plop itself in:
name->objects + to-add-b => {"foo" [{:name "foo" :x 1}] "bar" {:name "bar" :x nil}}

2021-03-30T20:11:29.007800Z

(s/setval
 [(:name object) s/AFTER-ELEM]
 object
 name->objects
 )
^ wow, above did the trick. this is awesome! great work