meander 2024-08-05

Hi Everyone, I started using meander for the first time. Here’s my use-case I am unable to figure out: I have a sample data:

(def sample-data {:xa.invoices/invoices [{:xa.invoice/id 1
                                            :xa.invoice/amount 100
                                            :xa.invoice/contact {:xa.contact/id 2
                                                                 :xa.contact/name "workd"}}
                                           {:xa.invoice/id 1
                                            :xa.invoice/amount 200
                                            :xa.invoice/contact {:xa.contact/id 2
                                                                 :xa.contact/name "hello"}}]})
I wish to extract keys from nested structure, without caring about structure itself. It output should be:
[{:xa.invoice/amount 100
  :xa.contact/name "workd"}
  {:xa.invoice/amount 200
   :xa.contact/name "hello"}]
I don’t wish to depend on the structure of the data, as the structure is unknown. I wish to extract value of the key whereever it is present in the data. Is this possible with meander?

I think m/$ is what you’re looking for. Also, I have found that walk/postwalk + meander is a very useful combination.

👍 1

I shifted to using tree-seq and flattening all the tuples into a flattened map, and just extract those keys. I will have to spend more time learn meander better.

I'm struggling to understand how to allow optionality in a nested map. Can somebody provide idiomatic meander syntax that will match either:

{:a {:b 1}
 :x 1}
or
{:a {:c 2}
 :x 2}
or
{:x 3}
and binds x to the value at it's top-level key and b and c to their respective nested keys' value and nil if not present? Consider that there may be many many more keys at the top level and in the nested map so enumerating all the possible combinations is not practical. My struggle is to allow b and c to be bound to nil (or, ideally, any default value) if the nested map is not present. For keys in the top level, optionality seems "built-in" in that variables are bound to nil if the key is not present but in a submap the entire pattern fails if the submap is not present.