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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning sub-map with select, not value #45

Closed
LusciousPear opened this issue Dec 24, 2015 · 2 comments
Closed

Returning sub-map with select, not value #45

LusciousPear opened this issue Dec 24, 2015 · 2 comments
Labels

Comments

@LusciousPear
Copy link

Hello! Been a while.

I'm writing some ETL code which basically filters a map by another map. Specter seems right for this. For example:

{:a 
  {:b {:c 4 :d 5} 
   :e {:f 6 :g 7} 
   :h {:i 8 :j 9} }}

filter by [ [:a :b :c] [:a :e :f] ]

 ---> {:a 
        {:b {:c 4}}
         :e {:f 6}}

I think the main thing I'm not understanding is getting select to return a sub-map, instead of the value. For instance (select [ALL :a :b :c]) returns "4" instead of {:a {:b {:c 4}}.

What concept am I missing?

@nathanmarz
Copy link
Collaborator

Here's one way to write your function:

(defn filter-map [paths input]
 (reduce
   (fn [m p] (setval p (select-one p input) m))
   {}
   paths
   ))

(filter-map [[:a :b :c] [:a :e :f]]
 {:a 
  {:b {:c 4 :d 5} 
   :e {:f 6 :g 7} 
   :h {:i 8 :j 9} }})
=>  {:a {:b {:c 4}, :e {:f 6}}}

If you precompile those paths this will execute very fast.

@LusciousPear
Copy link
Author

Interesting -- I didn't think of building a new map instead of filtering the original. Thanks!

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

No branches or pull requests

2 participants