Fork me on GitHub
#meander
<
2020-11-21
>
markaddleman20:11:42

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))

noprompt20:11:48

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.

noprompt20:11:03

(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}

noprompt20:11:32

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

noprompt20:11:55

(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]}

markaddleman21:11:44

Thanks, I was thinking about the second case

markaddleman21:11:35

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

noprompt21:11:02

Glad I could help. 🙂