👋 have you guys been down the road of converting core.match
functions to meander (🤞 and examples to hand)?
hey, I'm just getting started with meander and I'm already stuck (perhaps that's normal) 😄
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
basic example
{:x1 1
:x2 2}
;;=>
{:x1 "x1-1"
:x2 "x2-2"}
@michael.e.loughlin Are you looking for any ones in particular?
@plexus Its normal to take a minute to get up the first bit of the climb. Here are some ways to accomplish your goal.
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"}
You can also use rewrite
:
(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"}
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.