specter

Latest version: 1.1.3
2018-01-17T16:07:22.000050Z

Hey guys I have been scratching my head for some time now. I want to operate the following transformation :

["toto"
{:tag tag-to-unwrap :content ["titi" "tutu"]}
{:tag tag-to-unwrap :content ["toto"]}]
;=>
["toto" "titi" "tutu" "toto"]
Right now with:
(transform [ALL XML-ZIP NEXT-WALK NODE map? #(= :tag-to-unwrap (:tag %))]
:content
["toto"
{:tag tag-to-unwrap :content ["titi" "tutu"]}
{:tag tag-to-unwrap :content ["toto"]}])
I get
["toto" ["titi" "tutu"] ["toto"]]
Can I get specter to “splice” the vectors ["titi" "tutu"]and ["toto"]in the result?

nathanmarz 2018-01-17T16:20:04.000517Z

@jeremys with what's built-in you can do something like this:

(require '[com.rpl.specter.zipper :as z])

(def data
  ["toto"
    {:tag :a :content ["titi" "tutu"]}
    {:tag :a :content ["toto"]}]
  )

(transform
  [z/VECTOR-ZIP
   z/DOWN
   z/NEXT-WALK
   (selected? z/NODE map?)
   z/NODE-SEQ]
  #(select [FIRST :content ALL] %)
  data)

nathanmarz 2018-01-17T16:21:10.000250Z

it's also pretty easy to make an ALL-SEQ navigator which navigates to each element as a 1 element sequence, and then you could do:

(transform [ALL-SEQ (selected? FIRST map?)]
  #(select [FIRST :content ALL] %)
  data
  )

nathanmarz 2018-01-17T16:22:45.001072Z

technically this would work: (def ALL-SEQ (path z/VECTOR-ZIP z/DOWN z/NEXT-WALK z/NODE-SEQ))

nathanmarz 2018-01-17T16:22:54.000386Z

though you could make it more efficient with a custom navigator

nathanmarz 2018-01-17T16:25:46.000328Z

another solution:

(transform (continuous-subseqs map?)
  #(select [ALL :content ALL] %)
  data
  )

2018-01-17T16:27:47.000785Z

Thx @nathanmarz I’ll take a look and see what I can come up with.