Fork me on GitHub

you can control the created sequence type with NIL->VECTOR navigator


and (keypath (:name object)) will execute faster


so the path woudl be [(keypath (:name object)) NIl->VECTOR AFTER-ELEM]

❤️ 3
Franco Gasperino16:03:53

In the case where a transform operation takes a collection of one type, but is expected to return a collection of a different type, is transform the correct idiom?

(def data {:a {:required true} :b {:required false} :c {}})

(defn get-key-if-required [[k v]]
  (if (map? v)
    (let [r (:required v true)]
      (if (and (boolean? r) (true? r)) k nil))))

(map get-key-if-required data) => (:a nil :c)

; This does not work, likely expecting a map out
(specter/transform [specter/ALL] get-key-if-required data)


Okay, more complicated one:

data {:shelves {:name "foo" :books [{:id "a"} {:id "b"}]}
Now, I want to do the following:
shelf-task-a {:name "foo" :books [{:id "a"} {:id "c"}]}
shelf-task-b {:name "bar" :books [{:id "d"}]}
• Take shelf-task-a, if the shelf with that name exists, go into it’s books, otherwise create {:name x, :books []} • Then go into it’s :books, and for each book, if it exists, don’t add, otherwise append at the end I’m currently breaking this up into stages:
   (fn [data {:keys [found-book]}]
     (let [shelf-path [:shelves (s/not-selected? s/ALL #(= (:name %) shelf-name)) s/AFTER-ELEM]
           book-path  [:shelves s/ALL #(= (:name %) shelf-name) :books s/NIL->VECTOR 
                       (s/not-selected? s/ALL #(= (:id %) (:id found-book))) s/AFTER-ELEM]]
       (->> data
            (s/setval shelf-path {:name shelf-name})
            (s/setval book-path found-book))))

If you’d suggest something different am all ears 😄