(without actual context) Here's what I'd probably do. There's a bit of
repetition, but it's clear what's going on, so I don't mind.
assuming the my-map def from the original email.
user=> (defn select-nested-keys [m top-level-keys & {:as pairs}]
(reduce #(update-in %1 (first %2) select-keys (last %2)) (select-keys m
top-level-keys) pairs))
#'user/select-nested-keys
user=> (select-nested-keys my-map [:spouse :name :email :address]
[:address] [:state :city]
[:spouse] [:name :address]
[:spouse :address] [:state :city])
{:address {:city "San Francisco", :state "CA"}, :email "[email protected]", :name
"John Doe", :spouse {:address {:city "Atlanta", :state "GA"}, :name "Jane Doe"}}
The first vector is the top level keys, the following vectors are pairs of the
path to the k/v pair and which vals should be selected.
Cheers, Jay
On May 28, 2012, at 6:18 PM, Laurent PETIT wrote:
> Le 29 mai 2012 à 00:14, Jay Fields <[email protected]> a écrit :
>
>> use destructuring?
>
> I dont know, destructuring would, well, destructure the input map, giving you
> all the bits on the table, but unable to restructure it with the original
> shape.
>
> As I understand it, BG would like some kind of shape-preserving
> filter/dissoc.
>>
>> On May 28, 2012, at 6:12 PM, Laurent PETIT wrote:
>>
>>> Le 29 mai 2012 à 00:03, Laurent PETIT <[email protected]> a écrit :
>>>
>>>> Hello bg,
>>>> For describing the subsets you want, why not use as a bottom
>>>> representation something more connected with clojure datastructures?
>>>>
>>>> Like
>>>> set literal => set of sibling keys
>>>> Vector => path to an inner key
>>>> Map => I see no usage for maps right now
>>>>
>>>> Your example could then become:
>>>>
>>>> #{[:name]
>>>> [:email]
>>>> [:adress #{:city :state}]
>>>> [:spouse #{:name [:address #{:city :state}]}
>>>
>>> I don't have an implementation to offer, but as I see it, it could be some
>>> kind of superset of get-in, extending its syntax, if you will.
>>>>
>>>> What about that ?
>>>>
>>>>
>>>>
>>>> Le 26 mai 2012 à 06:59, Baishampayan Ghose <[email protected]> a écrit :
>>>>
>>>>> Hi,
>>>>>
>>>>> I have a problem wherein I need to select subsets of a given map;
>>>>> think about select-keys, but with arbitrary nesting.
>>>>>
>>>>> For example, consider this map -
>>>>>
>>>>> (def my-map {:name "John Doe"
>>>>> :email "[email protected]"
>>>>> :address {:house "42"
>>>>> :street "Moon St."
>>>>> :city "San Francisco"
>>>>> :state "CA"
>>>>> :zip 76509
>>>>> :mobile "+188888888"}
>>>>> :ssn "123456"
>>>>> :spouse {:name "Jane Doe"
>>>>> :ssn "654321"
>>>>> :relation :wife
>>>>> :address {:house "42"
>>>>> :street "Moon St."
>>>>> :city "Atlanta"
>>>>> :state "GA"
>>>>> :zip 76509
>>>>> :mobile "+188888888"}}})
>>>>>
>>>>> From the above map, I want to extract this sub-map -
>>>>>
>>>>> {:spouse {:name "Jane Doe", :address {:state "GA", :city "Atlanta"}},
>>>>> :name "John Doe",
>>>>> :email "[email protected]",
>>>>> :address {:state "CA", :city "San Francisco"}}
>>>>>
>>>>> What would be a good way to express this sub-map so that I can pass it
>>>>> on to a function?
>>>>>
>>>>> I am thinking about something like this -
>>>>>
>>>>> [:name :email {:address [:city :state]} {:spouse [:name {:address
>>>>> [:city :state]}]}]
>>>>>
>>>>> Example implementations are also welcome.
>>>>>
>>>>> Regards,
>>>>> BG
>>>>>
>>>>> --
>>>>> Baishampayan Ghose
>>>>> b.ghose at gmail.com
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Clojure" group.
>>>>> To post to this group, send email to [email protected]
>>>>> Note that posts from new members are moderated - please be patient with
>>>>> your first post.
>>>>> To unsubscribe from this group, send email to
>>>>> [email protected]
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/clojure?hl=en
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To post to this group, send email to [email protected]
>>> Note that posts from new members are moderated - please be patient with
>>> your first post.
>>> To unsubscribe from this group, send email to
>>> [email protected]
>>> For more options, visit this group at
>>> http://groups.google.com/group/clojure?hl=en
>>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en