meander

All things about https://github.com/noprompt/meander Need help and no one responded? Feel free to ping @U5K8NTHEZ
mloughlin 2019-11-05T10:27:25.192600Z

👋 have you guys been down the road of converting core.match functions to meander (🤞 and examples to hand)?

plexus 2019-11-05T15:02:39.193200Z

hey, I'm just getting started with meander and I'm already stuck (perhaps that's normal) 😄

plexus 2019-11-05T15:03:56.194500Z

I have a map with beforehand unknown keys, I want to create a new map with the same keys, and with values based on the key and value of the source map

plexus 2019-11-05T15:04:38.194800Z

basic example

{:x1 1
 :x2 2}
;;=>
{:x1 "x1-1"
 :x2 "x2-2"}

noprompt 2019-11-05T17:03:52.195600Z

@michael.e.loughlin Are you looking for any ones in particular?

noprompt 2019-11-05T17:11:51.196600Z

@plexus Its normal to take a minute to get up the first bit of the climb. Here are some ways to accomplish your goal.

noprompt 2019-11-05T17:12:05.197Z

You can start with a search:

;; 3. Insert new entries into map.
(into {} (m/search {:x1 1 :x2 2}
           {?k ?v} ;; 1. Match/bind all keys and values
           ;; 2. Create a new entry.
           [?k (str (name ?k) "-" ?v)]))
;; =>
{:x2 "x2-2", :x1 "x1-1"}

noprompt 2019-11-05T17:12:32.197600Z

You can also use rewrite:

noprompt 2019-11-05T17:12:34.197800Z

(m/rewrite {:x1 1 :x2 2}
  {;; 1.  Match/bind a key and value,
   ?k ?v
   ;; 2.  recursively match/rewrite on remainder of map.
   & (m/cata ?m)}
  ;; 3. Construct new map with the key and new value and the rewritten
  ;; version of `?m`.
  {?k ~(str (name ?k) "-" ?v)
   & ?m}

  ;; Default empty map.
  {:as ?m}
  ?m)
;; =>
{:x1 "x1-1", :x2 "x2-2"}

jimmy 2019-11-05T17:15:18.200200Z

One other way. I think the first way above is probably best, but just want to show some other ideas in meander.

(m/rewrite {:x1 1
           :x2 2}
  (m/seqable [(m/and !ks !kks) !vs] ...)
  {& [[!ks (m/app str (m/app name !kks) "-" !vs)] ...]})
I think this is one example of where we could make things a bit better. But this is also not what meander is really focused on a the moment. I think you'll find if you are trying to do specific data transformation rather than very generic ones, meander will be easier to understand.