Fork me on GitHub
#specter
<
2021-03-30
>
stopa18:03:02

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?

nathanmarz18:03:35

@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))

stopa20:03:10

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}}

stopa20:03:29

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