This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-08-28
Channels
- # announcements (2)
- # babashka (36)
- # beginners (29)
- # bristol-clojurians (2)
- # calva (3)
- # cider (102)
- # circleci (7)
- # clj-kondo (5)
- # cljs-dev (7)
- # clojure (117)
- # clojure-europe (23)
- # clojure-korea (1)
- # clojure-nl (1)
- # clojure-spec (15)
- # clojure-uk (47)
- # clojurescript (43)
- # code-reviews (1)
- # community-development (1)
- # conjure (32)
- # cursive (1)
- # datalog (15)
- # datomic (14)
- # emacs (18)
- # fulcro (9)
- # helix (23)
- # jackdaw (1)
- # jobs-discuss (10)
- # meander (8)
- # membrane (57)
- # off-topic (4)
- # portal (2)
- # re-frame (22)
- # reagent (1)
- # reitit (9)
- # reveal (3)
- # rewrite-clj (14)
- # shadow-cljs (22)
- # spacemacs (27)
- # sql (34)
- # testing (6)
- # tools-deps (40)
- # vim (5)
- # vrac (15)
- # xtdb (2)
Hello, I have a noob question if you don’t mind. I have some code like this:
(def example {:a {:ret :int}
:b {:ret :int}
:c {:ret :bool}})
(reduce (fn [acc [k v]]
(update acc k (fnil conj []) v))
{}
(m/search example
{?v {:ret ?k}}
[?k ?v]))
Which is a inversion of a map, sort of. I am wondering, is there some way to express this as a rewrite?Use group-by
for this. 🙂
(let [example {:a {:ret :int}
:b {:ret :int}
:c {:ret :bool}}]
(group-by first (m/search example {?v {:ret ?k}} [?k ?v])))
Looks like those aren't quite equivalent. Going to take a look more at what is intended
Oops, yep you’re right
(let [example {:a {:ret :int}
:b {:ret :int}
:c {:ret :bool}}]
(m/rewrite [example {}]
[{?v {:ret ?k} & ?rest} ?out]
(m/cata [?rest (m/cata (`append ?out ?k ?v))])
(`append {?k [!vs ...] :as ?out} ?k ?v)
{?k [!vs ... ?v] & ?out}
(`append ?out ?k ?v)
{?k [?v] & ?out}
[_ ?out]
?out))
;; =>
{:int [:a :b], :bool [:c]}
Thank you for the answer! It’s funny squinting at it, it looks like logic programming to me. I don’t know the cata
operator yet besides the vague idea of recursion. Now I see a bit better how it works and the trick building the append` list to recur is neat. Wouldn’t have found it myself that’s for sure. Thanks
Oops, yep you’re right
(let [example {:a {:ret :int}
:b {:ret :int}
:c {:ret :bool}}]
(m/rewrite [example {}]
[{?v {:ret ?k} & ?rest} ?out]
(m/cata [?rest (m/cata (`append ?out ?k ?v))])
(`append {?k [!vs ...] :as ?out} ?k ?v)
{?k [!vs ... ?v] & ?out}
(`append ?out ?k ?v)
{?k [?v] & ?out}
[_ ?out]
?out))
;; =>
{:int [:a :b], :bool [:c]}
@jeremys In general, right now in meander if you want to group things, the general answer is to use clojures means of grouping.
We hope to have a better answer in the future.