This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-02-03
Channels
- # announcements (1)
- # babashka (31)
- # babashka-sci-dev (53)
- # beginners (34)
- # calva (54)
- # cider (15)
- # clj-kondo (9)
- # clojure (115)
- # clojure-dev (19)
- # clojure-europe (21)
- # clojure-nl (1)
- # clojure-norway (78)
- # clojurescript (10)
- # clr (9)
- # community-development (9)
- # core-async (24)
- # cursive (18)
- # datomic (59)
- # emacs (43)
- # figwheel-main (2)
- # fulcro (4)
- # graphql (4)
- # malli (7)
- # meander (12)
- # nbb (14)
- # off-topic (22)
- # polylith (9)
- # re-frame (5)
- # reitit (3)
- # releases (1)
- # shadow-cljs (36)
- # sql (1)
- # tools-build (23)
- # xtdb (13)
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}]
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
btw ctrl+shift+enter
or type /snippet
to create a collapsible snippet that can have syntax highlighting
cool didn't know that 👍
This works