Fork me on GitHub
#specter
<
2022-05-18
>
Matthew Davidson (kingmob)03:05:31

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
isak16:05:10

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

isak16:05:01

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 (kingmob)04:05:44

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