Fork me on GitHub
#meander
<
2021-07-30
>
noprompt17:07:11

@garyberger Are you looking for something like this?

(let [json {"?bar" [1 "?foo" 2]
            "userName" "?user-name"}]
  (m/rewrite json
    ;; Arrays
    [(m/cata !x) ...]
    [!x ...]

    ;; Objects
    {(m/cata ?k) (m/cata ?v) & (m/cata ?rest)}
    {?k ?v & ?rest}

    ;; Logic variables
    (m/re #"\?[^\s]+" ?variable)
    (m/symbol ?variable)

    ;; Else
    ?x ?x))
;; => 
{"userName" ?user-name
 ?bar [1 ?foo 2]}

Gary Berger17:07:43

Yes exactly, spectacular.. I was just experimenting with m/rewrite to do that and needed to figure out the recursion.. Thanks so much!! this is gold

noprompt18:07:49

Cool. You can find more information (by example) about cata in the cookbook: https://github.com/noprompt/meander/blob/epsilon/doc/cookbook.md

noprompt18:07:42

Also, please make sure to use version 0.0.602 of meander/epsilon as the latest release is broken in a few ways.

Gary Berger19:08:43

Sorry to be a pain on this but can you use

(m/rewrite in
    ;; ;; Arrays
               [(m/cata !x) ...]
               [!x ...]
    ;; Objects
               {(m/cata ?k) (m/cata ?v) & (m/cata ?rest)}
               {?k ?v & ?rest}
    ;; Logic variables
               (m/re #"\?[^\s]+" ?variable)
               (m/symbol ?variable)
    ;; Else
               ?x ?x) 
inside of a m/match I am getting a
Syntax error macroexpanding meander.match.epsilon/match at ...
; When matching, map patterns may not contain variables in their keys that would make it so there is more than one match possible.
I tried using it as a function but the evaluation fails

noprompt19:08:56

Yes. m/match does not allow ambiguous patterns. A map pattern with a variable key ie.

{?x _}
is ambiguous because maps have no canonical representation, and because a map patterns are actually submap patterns. The pattern above means “a map of at least size one with the key ?x”. For any map of size greater than one, which key do we bind to ?x? Meander can’t say. So for m/match this is a problem and hence the error. However, m/find is available for this purpose, but it doesn’t default to throwing an error when all the patterns have been exhausted.

👀 2