specter

Latest version: 1.1.3
nathanmarz 2021-03-31T03:25:42.008300Z

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

nathanmarz 2021-03-31T03:25:58.008800Z

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

nathanmarz 2021-03-31T03:26:49.009900Z

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

❤️ 1
2021-03-31T16:03:00.010600Z

Nice!

Franco Gasperino 2021-03-31T16:12:53.016600Z

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)

2021-03-31T16:20:45.017800Z

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:
(reduce
   (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))))

   data
   import-tasks)
If you’d suggest something different am all ears 😄