specter

Matthew Davidson 2022-05-18T03:15:31.676159Z

Thanks @isak. I feel like I need some mid-level documentation, something in-between a high-level overview, a list of examples, and the API reference. I haven't seen anything that explains collecting well to me.

👍🏻 1
isak 2022-05-18T16:46:10.674489Z

Yea I didn't see anything either. I had to experiment a little to understand it, and I think I'm starting to now. This example may help a little:

(def root
  {:level-1-foo {:level-2-bar {:data "a"}
                 :level-2-baz {:data "b"}}
   :level-1-bar {:level-2-bar {:data "c"}
                 :level-2-baz {:data "d"}
                 :level-2-foo {:data "e"}}
   :level-1-baz {:level-2-bar {:data "c"}
                 :level-2-baz {:data "d"}}})

(x/transform
  [(x/collect-one (x/view count)) ; root-count
   x/MAP-VALS
   (x/collect (x/view count))     ; level-1-entry-count
   x/MAP-VALS]
  (fn [root-count level-1-entry-count leaf]
    (assoc leaf 
      :root-count root-count
      :level-1-entry-count level-1-entry-count))
  root)

; =>

{:level-1-foo {:level-2-bar {:data "a", :root-count 3, :level-1-entry-count [2]},
               :level-2-baz {:data "b", :root-count 3, :level-1-entry-count [2]}},
 :level-1-bar {:level-2-bar {:data "c", :root-count 3, :level-1-entry-count [3]},
               :level-2-baz {:data "d", :root-count 3, :level-1-entry-count [3]},
               :level-2-foo {:data "e", :root-count 3, :level-1-entry-count [3]}},
 :level-1-baz {:level-2-bar {:data "c", :root-count 3, :level-1-entry-count [2]},
               :level-2-baz {:data "d", :root-count 3, :level-1-entry-count [2]}}}

isak 2022-05-18T16:49:01.531139Z

So it looks like it is for when you want to transform nested nodes, and you want to carry context deeper in the tree. Each collect you do will increase the arity of the transform function, and the arguments are prepended left to right.

Matthew Davidson 2022-05-19T04:28:44.437149Z

So collects take place at the position of the path so far. > Each collect you do will increase the arity of the transform function, and the arguments are prepended left to right. It would be cool if they could be named, and then passed in as a map

👍🏻 1