Fork me on GitHub
#meander
<
2023-02-03
>
hanDerPeder11:02:16

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

xificurC11:02:53

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

hanDerPeder12:02:44

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
   ...?
   }
  )

hanDerPeder12:02:18

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

hanDerPeder12:02:43

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 element

xificurC13:02:58

tbh in this case I'd go with a partial solution like below. Not sure if meander gives you much here though

hanDerPeder13:02:44

thanks for the help!

👍 2
xificurC13:02:02

btw ctrl+shift+enter or type /snippet to create a collapsible snippet that can have syntax highlighting

hanDerPeder13:02:37

cool didn't know that 👍