Fork me on GitHub
#specter
<
2023-12-15
>
nuriaion15:12:36

Hi I'm trying to transform the output from instaparse:

[:S
 [:game
  [:id "1"]
  [:draws
   [:draw
    [:cubes "3" "blue" "4" "red"]
    [:cubes "1" "red" "2" "green" "6" "blue"]
    [:cubes "2" "green"]]]]]
to something like:
{:id "1",
 :draws
 [[{:number "3", :color "blue"}
   {:number "3", :color "blue"}]
  [{:number "1", :color "red"}
   {:number "2", :color "green"}
   {:number "6", :color "blue"}]
  [{:number "2", :color "green"}]]}
so far i was only able to acces the data via index:
(defn get-data [game]
  (let [id (s/select-any [(s/nthpath 1 1 1)] game)
        games (s/select [s/nthpath 1 2 1] game)]
    {:id id :games games}))
i would prefer to have some path like [SELECTOR :id] etc. Could you tell me in which direction i should go there?

mmer16:12:04

You might like to look at using transform that will take a function that returns the transformation. in this case the function could take the vector [:cubes "3" "blue" "4" "red"] and transform it into the vector

[{:number "3", :color "blue"}
   {:number "4", :color "red"}]
I also wonder if it would help to transform the vectors directory within the :game vector into a map before doing the transformation. That would give you the :id and and :draws keys before trying the transformation. I will add that I notice loads of these kind of questions and looking at your data the fact that it does not easily transform into an obvious map is difficult.

nuriaion19:12:26

Thanks for your help! I saw now https://clojurians.slack.com/archives/C0FVDQLQ5/p1529932554000085 and understod that this is the simpler solution for me.