Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to continue recursive process on a transformed object #293

Open
caumond opened this issue Aug 12, 2020 · 0 comments
Open

How to continue recursive process on a transformed object #293

caumond opened this issue Aug 12, 2020 · 0 comments

Comments

@caumond
Copy link

caumond commented Aug 12, 2020

Hi guys,
Really impressed by the high quality of this library. Congrats. 馃

A little bit hard to use at the beginning, but I'm still a little bit new to clojure, and I start with non-trivial cases 馃槣

I'm parsing a json structure (maps and collections), in which I want to drop some intermediate levels but keep the subtree: so I expect a submap to be move at a higher level.

I achieved the following recursive path, finding exactly what I am seeking:

(defn SELECT-ATTRIBUTES-PATH [list-attrs]
  (recursive-path [] p
                  (multi-path
                   [map? (compact (submap list-attrs)) ]
                   [coll? (compact ALL) p]
                   ))
  )

But when trying to achieve my modifications, I didn't find a better way than doing it in vanilla clojure:

(defn nested-maps-to-attribute [maps]
  (when (map? maps)
    (apply merge
           (for [[_ nested-map] maps]
             (when (map? nested-map)
               nested-map)
             ))))

and then apply it with a transform:

(transform (SELECT-ATTRIBUTES-PATH #{:a :j})
           nested-maps-to-attribute
           {:a :b
            :f 0
            :g [1 2]
            :h "foo"
            :c {:d :e
                :j {:y :z}
                :a :c
                :i {}
                :h [ :i :j]
                :k [ {:l :m :n :o} {:p :q}]}}
           )

This works exactly as wanted, dropping :a even if nested, and moving :j submap to the upper level:

{:f 0,
 :g [1 2],
 :h "foo",
 :c {:d :e, :h [:i :j], :k [{:l :m, :n :o} {:p :q}], :y :z}}

My issue is that I would like to apply recursively these transformations, so the following:

(transform (SELECT-ATTRIBUTES-PATH #{:c :j})
           nested-maps-to-attribute
            {:c { :j {:y :z}}}
 )

should move [:y :z] directly at the root map --> {:y :z}

Instead, I find {:j {:y :z}.

Please help 馃啒 , I don't even know where to search...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant