Fork me on GitHub
#meander
<
2020-03-19
>
rfhayashi15:03:05

Hi! Is there a way in Meander to transform:

{:items {:item1 {:value 1}
         :item2 {:value 2}}}
into:
{:values {:item1 1
          :item2 2}}
? I tried:
(m/search
 {:items {:item1 {:value 1}
          :item2 {:value 2}}}
 {:items {?item {:value ?value}}}
 {:values {?item ?value}})
But I get:
({:values {:item2 2}} {:values {:item1 1}})
I also tried match (which complains of logic variables in the key position) and rewrite, but could not figure out a way. I know I can do a merge-with on top of the result, but curious if there is a way to do this directly with Meander. Thanks!

magnusdk15:03:40

I came up with this:

(m/match
  {:items {:item1 {:value 1}
           :item2 {:value 2}
           :item3 {:value 3}}}
  {:items (m/seqable [!item {:value !value}] ...)}
  {:values (zipmap !item !value)})
but I’m still new to Meander so I can’t tell if this is the most idiomatic way or not :man-shrugging:

Jimmy Miller16:03:45

Repeats in maps is a little ugly right now. That will be fixed with https://github.com/noprompt/meander/pull/117 But in the mean time you can do this

(m/rewrite {:items {:item1 {:value 1}
                    :item2 {:value 2}}}
  {:items {& (m/seqable [!ks {:value !vs}] ...)}}
  {:values {& [[!ks !vs] ...]}})
I can explain this in more detail later.

Jimmy Miller16:03:26

Once this lands all you'd have to do is.

(m/rewrite {:items {:item1 {:value 1}
                    :item2 {:value 2}}}
  {:items (m/map-of !ks {:value !vs})}
  {:values (m/map-of !ks !vs)})

👍 4
Jimmy Miller20:03:50

This has now been merged if you update your meander to “0.0.408” you can now use map-of

rfhayashi21:03:12

🆒 Thank you for the help!

Jimmy Miller20:03:10

New version: meander/epsilon {:mvn/version "0.0.408"} * Adds map-of operator. * Removes clojurescript dependency * Fixes a bug in a :ret spec.

party-corgi 4
partywombat 4
parrot 8