Fork me on GitHub
#meander
<
2020-03-01
>
timothypratley02:03:38

(m/match
  {:a 1
   :b 2}
  {:a !v
   & (m/or {:b !v}
           {})}
  !v)
^^ is this the “right” way to do optional keys?

Jimmy Miller02:03:48

Any reason not to do just (m/or nil !v)?

timothypratley03:03:23

I’m not following :thinking_face: I’m matching something like {:methods [1 2 3] :resources {:methods [4 5 6]}} where a resource has methods and optionally a sub resource with methods etc and am using this:

(def extract-resource-methods
  (s/rewrite
    (m/with [%resource {:methods {& (m/seqable [_ !methods] ...)}
                        & (m/or {:resources {& (m/seqable [_ %resource] ...)}}
                                {})}]
      [?baseUrl %resource])
    ;;=>
    [[?baseUrl !methods] ...]))
to represent that :resources may or may not be present… it just seems a little wierd like I’m over complicating it.

timothypratley06:03:20

I guess more importantly I don’t see a way to do many optional keys

timothypratley07:03:19

oh I think I see…

(m/match
  {:a 1}
  {:a ?a
   :b ?b}
  [?a ?b])
=> [1 nil]
^^ I didn’t realize that meander matches patterns even if not all keys are present hmmm. :thinking_face: I guess then everything is optional unless I do a pred or guard. 🙂

Jimmy Miller14:03:57

Yes everything is optional by default. my answer was assuming you just didn't want the nil to be in your memory variable. Technically it isn't just guard or pred. If you put a vector pattern for example that means you are asserting that there is some non nil vector there.

noprompt21:03:12

[meander/epsilon "0.0.401"]
• Fixes some issues with or pattern compilation • Fixes uses of transients in substitution code

Jimmy Miller22:03:52

The or stuff didn't actually land in this release. But we will cut a new one soon with the or fix.

👍 8