This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-03-29
Channels
- # announcements (4)
- # architecture (25)
- # babashka (23)
- # beginners (56)
- # boot (4)
- # calva (1)
- # cider (1)
- # cljs-dev (15)
- # clojure (135)
- # clojure-europe (4)
- # clojure-nl (21)
- # clojure-uk (61)
- # clojurescript (47)
- # community-development (4)
- # cryogen (11)
- # cursive (11)
- # data-science (11)
- # fulcro (25)
- # funcool (5)
- # graalvm (1)
- # graphql (4)
- # kaocha (4)
- # lambdaisland (20)
- # lumo (7)
- # meander (14)
- # off-topic (9)
- # pathom (2)
- # portkey (1)
- # re-frame (5)
- # reagent (53)
- # reitit (2)
- # ring (3)
- # shadow-cljs (32)
- # spacemacs (4)
- # sql (34)
- # testing (1)
- # tools-deps (3)
In this case the usual recommendation is to do the group-by
first and then your processing after.
(let [data [{:class :a, :numbers [10], :count? true}
{:class :b :numbers [3], :count? true}
{:class :a, :numbers [88], :count? true}]]
(m/find (group-by :class data)
{?class [(m/or {:numbers [!nums ...] :count? true} _) ...] & (m/cata ?rest)}
(cons {:class ?class :numbers (reduce + !nums) :count? true} ?rest)
{}
()))
;; =>
({:class :a, :numbers 98, :count? true}
{:class :b, :numbers 3, :count? true})
Aggregation is something that comes up a lot and we will have something specifically for it available by summer (along with other things).
I spent some time tracking down a bug last week that turned out to be due to my use of the (and !memvar1 !memvar2)
method from the cookbook for re-using a memvar in two different places in the output pattern, coupled with the ..!n
syntax
something like [(m/and !m1 !m2) ..!n]
-> {:a [!m1 ..!n] :b [!m2 ..!n]}
, except nested in a much more complicated pattern
The bug was pretty obvious once discovered but it occured to me that such constructs can be pretty hard to reason about in subtle ways.. with an imperative flavour of pushing and popping from arrays and keeping track of indices
The next logical step is asking whether there exists some sort of ..(and !n1 !n2)
syntax , but that just seems like a further step in the wrong direction..
really the [[!x ..!n] ...]
syntax is a way of fitting multidimensional data into a linear memory variable, I wonder if there could be a less clunky way of encoding this
With fold and unfold we will have more power to implement better ways of doing these things. But my recommendation is also to dream up your ideal way and share it. Maybe it will be implemented or maybe it will spark other with more ideas.
Question about variable number of forms:
;; This works:
(def data
'((path "/a"
(PUT "Updates A"))
(path "/b"
(GET "Gets a B"))))
(m/rewrite
data
((path !path (!method !desc)) ..!paths)
[{:path !path
:routes
{:method !method
:desc !desc}} ..!paths])
;; But this was harder, when adding more routes to a path:
(def data
'((path "/a"
(PUT "Updates A"))
(path "/b"
(PUT "Gets a B")
(PUT "Updates B"))))
;; Desired output
[{:path "/a" :routes [{:method 'PUT :desc "Updates A"}]}
{:path "/b" :routes [{:method 'GET :desc "Gets a B"}
{:method 'PUT :desc "Updates B"}]}]
I’ve tried to make it seqable and some other things, but can’t find the way to make this work. What am I missing?(let [data
'((path "/a"
(PUT "Updates A"))
(path "/b"
(PUT "Gets a B")
(PUT "Updates B")))]
(m/rewrite
data
((path !path . (!method !desc) ..!routes) ..!paths)
[{:path !path
:routes [{:method !method
:desc !desc} ..!routes]} ..!paths]))
;; =>
[{:path "/a",
:routes [{:method PUT, :desc "Updates A"}]}
{:path "/b",
:routes [{:method PUT, :desc "Gets a B"}
{:method PUT, :desc "Updates B"}]}]
Keep in mind you can always use cata
to do nested transforms with separate rules rather than all in one shot.
Ah… I was more or less in dot from finding the solution then! Close, but no cigar! 🙂 Thank you very much, @noprompt!