meander

All things about https://github.com/noprompt/meander Need help and no one responded? Feel free to ping @U5K8NTHEZ
2020-02-26T17:47:29.001800Z

Hi! Is there a good way to get from

[{:name "Alice" :id 1}
 {:name "Bob" :id 2}]
to
{1 "Alice"
 2 "Bob"}
? Match doesn't let me scan and search returns two maps
(m/search [{:name "Alice" :id 1}
           {:name "Bob" :id 2}]
          (m/scan {:name ?name :id ?id})
          {?name ?id})

jimmy 2020-02-26T18:36:29.005300Z

With rewrite you can do the following:

(m/rewrite [{:name "Alice" :id 1}
            {:name "Bob" :id 2}]
  [{:name !names :id !ids} ...] 
  {& [[!names !ids] ...]})
I should probably put this example in the cookbook. Sadly with maps ... isn't straight forward. But the {& [[x y] ...]} is a nice little idiom for building up maps. Also, just to mention the alternative possibility, if you really needed search you could use into.
(into
 {}
 (m/search [{:name "Alice" :id 1}
            {:name "Bob" :id 2}]
   (m/scan {:name ?name :id ?id})
   {?name ?id}))
My recommendation is trying out rewrite for this.

jimmy 2020-02-26T18:37:33.005500Z

Just throw more ways out. You can do this with match and some pretty standard clojure.

(m/match [{:name "Alice" :id 1}
          {:name "Bob" :id 2}]
  [{:name !names :id !ids} ...] 
  (into {} (map vector !ids !names)))

2020-02-26T18:39:48.005700Z

Thanks a lot for the rewrite suggestion, I wouldn't have come up with that one myself 🙂 It's of course easy to convert it with into, but how fun is that? Seriously it does loose some of the what you see is what you get-ness.

😆 1
jimmy 2020-02-26T18:46:25.006Z

Yeah, and the & solution for maps isn't the most wysiwyg thing either. Sadly we couldn't think of a great way to do repeats since maps have to always have pairs and are un-ordered. A perennial topic that comes up is adding a map-of operator. But we've yet to settle on an implementation. https://github.com/noprompt/meander/issues/74

2020-02-26T18:50:28.006400Z

(m/match [{:name "Alice" :id 1}
          {:name "Bob" :id 2}]
         [{:name !names :id !ids} ...]
         (zipmap !names !ids))

👍 1
2020-02-26T18:50:43.006600Z

yet another version