I'm struggling translating this transform function to meander. It transform a list of heterogenous values into a different representation. Tried using scan with memory variables, but then I basically have to reimplement the mapping function in the meander expression. Any pointers?
(def data [{:value "hello"} {:foo 123}])
(defn transform [data]
(mapv
(fn [{:keys [value foo]}]
(if value
{:type :a :value value}
{:type :b :foo-amount foo}))
data))
(transform data) ; => [{:type :a, :value "hello"} {:type :b, :foo-amount 123}]This works
have a look at m/rewrites. Something like
(m/rewrites data
{:value (m/pred some? ?v)} {:type :a, :value ?v}
{:foo (m/pred some? ?foo)} {:type :b, :value ?foo})nice thanks. the data list is actually nested inside some larger structure. can I inline rewrites? something like
(def data {:title "hello"
:list
[{:value "hello"} {:foo 123}]})
(match data
{:title ?title
:list (rewrite
{:value (pred some? ?v)} {:type :a :value ?v}
{:foo (pred some? ?foo)} {:type :b :foo-amount ?foo})}
{:the-title? ?title
...?
}
)or
(match data
{:title ?title
:list ?list}
{:the-title? ?title
:new-list (m/rewrites ?list
{:value (m/pred some? ?v)} {:type :a :value ?v}
{:foo (m/pred some? ?foo)} {:type :foo :foo-amount ?foo})})hmm, can't seem to get this to work this works
(mean/rewrites {:value "hello"}
{:value (mean/pred some? ?v)} {:type :a :value ?v}
{:foo (mean/pred some? ?foo)} {:type :b :foo-amount ?foo}) ; => ({:type :a, :value "hello"})
and this gives the same output
(mean/rewrites [{:value "hello"} {:foo 123}]
[{:value (mean/pred some? ?v)} & _] {:type :a :value ?v}
[{:foo (mean/pred some? ?foo)} & _] {:type :b :foo-amount ?foo}) ; => ({:type :a, :value "hello"})
but not sure how to apply this for each elementhttps://github.com/noprompt/meander/blob/epsilon/doc/cookbook.md#rewrite-sequence-of-maps
tbh in this case I'd go with a partial solution like below. Not sure if meander gives you much here though
thanks for the help!
btw ctrl+shift+enter or type /snippet to create a collapsible snippet that can have syntax highlighting
cool didn't know that 👍