meander

All things about https://github.com/noprompt/meander Need help and no one responded? Feel free to ping @U5K8NTHEZ
grounded_sage 2020-04-23T14:44:01.140400Z

How do I do this where :Price is an optional key. I have certain JSON files where say :Price or perhaps :Name inside of :Price is missing. I want to still match on the file and offer a default value

(defn data-transform
  [data]
  (m/search data
            {:Data {:Tickets (m/scan {:Seat ?seat
                                      :Eventname ?event-name
                                      :Price {:Name ?price-name
                                              :Amount ?price-amount}})}}
            {:seat ?seat
             :ticket-category (or ?price-name nil)
             :price (or ?price-amount nil)}))

jimmy 2020-04-23T16:38:14.140800Z

Do you actually need search semantics for some bigger part of this. Or are you okay with it being a match?

jimmy 2020-04-23T16:40:02.141Z

Actually I can make it work with search.

(m/search data
          {:Data {:Tickets (m/scan {:Seat ?seat
                                    :Eventname ?event-name
                                    :Price (m/or
                                            {:Name ?price-name
                                             :Amount ?price-amount}
                                            (m/let [?price-name "default-name"
                                                    ?price-amount "default-price"]
                                              nil))})}}
  {:seat ?seat
   :ticket-category ?price-name
   :price ?price-amount})

grounded_sage 2020-04-23T17:08:32.142500Z

Iā€™m having some internet troubles atm so have to reply on phone. What would you do if :Price itself is missing?

noprompt 2020-04-23T17:10:35.142700Z

The answer @jimmy gave accounts for that :Price , (m/or {,,,} (m/let [,,,]))

noprompt 2020-04-23T17:11:10.142900Z

So either :Price is a map and you bind the values, or you m/let them out.

šŸ‘ 1
grounded_sage 2020-04-23T17:57:56.143800Z

I actually think the solution you gave me will be sufficient. But I am curious as to how one would handle missing data like I described

jimmy 2020-04-23T18:10:43.144Z

Yeah sorry I missed all the other ones. I will try to write that up as well.

jimmy 2020-04-23T19:38:14.144400Z

I should explain the steps on how I got here, but I already wasted too much time playing with this šŸ™‚

(m/defsyntax with-defaults [bindings body]
  (m/rewrite bindings
    [!binding (m/and !lvar1 !lvar2) !default ...]
    ('m/with [!binding ('m/or ('m/some !lvar1)
                        ('m/let [!lvar2 !default] nil))
              ...]
     ~body)
    ))

(m/search data
  (with-defaults [%name ?name "name"
                  %amount ?amount 0]
    {:Data {:Tickets (m/scan {:Seat ?seat
                              :Eventname ?event-name
                              :Price (m/or
                                      {:Name %name
                                       :Amount %amount}
                                      (m/and nil
                                             %name
                                             %amount))})}})
  {:seat ?seat
   :ticket-category ?name
   :price ?amount})
Doing it all inline was getting too verbose. So I made my own little defsyntax thing.