Given the following tree:
{:children [:a {:children [:aa {:children [:aa {}]}]}
:b {:children [:bb {}]}]}
I'm trying to add a hashed id of the path of each element.
That's the path I wrote:
(def a
(sp/recursive-path [] p
[(sp/must :children)
(sp/view (partial partition 2))
sp/ALL
(sp/collect-one sp/FIRST)
sp/LAST
(sp/stay-then-continue p)]))
it works as expected for select with collected values:
(sp/select a {:children [:a {:children [:aa {}]}
:b {:children [:bb {}]}]})
=> [[:a {:children [:aa {}]}] [:a :aa {}] [:b {:children [:bb {}]}] [:b :bb {}]]
But when transformed, it changes the vector type of :children to nested lists (probably because of the sp/view?)
(sp/transform a
(fn [& args] (let [o (last args)
h-id (-> args butlast hash)]
(-> o
(assoc :h-id h-id))))
{:children [:a {:children [:aa {}]}
:b {:children [:bb {}]}]})
=> {:children ((:a {:children ((:aa {:h-id -149183532})), :h-id -237810920}) (:b {:children ((:bb {:h-id -876750745})), :h-id -1995900215}))}
How can I modify a view back to its original value?
When I'm printing o inside the transform function, the children is a valid vector and not a nested listdid you try using parser ?
(parser #(partition 2 %) #(into [] (mapcat identity) %)) instead of view
thanks ill give it a shot