meander

All things about https://github.com/noprompt/meander Need help and no one responded? Feel free to ping @U5K8NTHEZ
markaddleman 2020-11-21T20:30:42.391800Z

I'm having some trouble understanding how map-of works. How would I use it to produce a map of keys to a vector of odd numbers?

(m/rewrite {:a [1 2 3]
              :b [4 5 6]}
    (m/map-of !ks ___)
    (m/map-of !ks !vector-of-odds))

noprompt 2020-11-21T20:40:48.392200Z

map-of matches all entries with the key and value pattern respectively, for substitution it builds a map from the key and value patterns respectively.

noprompt 2020-11-21T20:41:03.392400Z

(me/rewrite {:a [1 2 3]
             :b [4 5 6]}
  (me/map-of !ks (me/gather (me/pred odd? !odds)))
  (me/map-of !ks !odds))
;; =>
{:a 1, :b 3}

noprompt 2020-11-21T20:42:32.392600Z

If you’re interested in mapping the keys to their respective odd numbers that would require a bit more effort.

noprompt 2020-11-21T20:43:55.392800Z

(me/rewrite {:a [1 2 3]
             :b [4 5 6]}
  {?k (me/gather (me/pred odd? !odds)) & ?rest-map}
  {?k [!odds ...] & (me/cata ?rest-map)}

  ?x
  ?x)
;; =>
{:b [5], :a [1 3]}

markaddleman 2020-11-21T21:03:44.393Z

Thanks, I was thinking about the second case

markaddleman 2020-11-21T21:04:35.393200Z

I see that map-of was the wrong direction to go

noprompt 2020-11-21T21:08:02.393400Z

Glad I could help. 🙂