Hi, is it possible to aggregate data in rewrites?
(m/rewrites data
(m/$ {:connections (m/some {:active ?a :created ?c})})
{:active (sum ?a) :created (sum ?c)}))Not really. You can use m/app as an escape hatch to reduce / aggregate data.
I hear meander zeta will be able to handle this situation (friendly poke @noprompt)
but i have no idea how to do that even with m/app
Something like this:
(m/rewrites data
(m/$ {:connections (m/some {:active !a :created !c})})
{:active (m/app (fn[as] (reduce + as)) [!a ...])}))
Notice the use of ! instead of ?
The ! symbol indicates a memory variable which will collect all of the values. [!a …] expands the memory variable to a vector which is then passed into the function
Does that make sense?
@markaddleman thank you, for explanation!
am trying that:
(let [sum #(reduce + 0 %)]
#(m/rewrites %
(m/$ {:connections (m/some {:active !a :created !b})})
{:active (m/app sum [!a ...])
:created (m/app sum [!b ...])})))it returns array of maps instead of single map
I think you want to use m/rewrite instead of m/rewrites
but i need to aggregate all {:active !a :created !b}
inside that data structure
Thank you for explaining.
The m/app sum… operation is applied at each match.
You want to sum over all matches. Is that correct?
that way it works as expected:
#(apply merge-with +
(m/rewrites %
(m/$ {:connections (m/some {:active ?a :created ?c})})
{:active ?a :created ?c}))"You want to sum over all matches. Is that correct?" yes
Yes, that makes sense. I apologize - I assumed a different datastructure
np )
To explain: The scope of a memory variable is each match. In your case, the memory variable is reset at each {:activity _ :created _} so my solution does not work for you
thats clear, thanks! then i will go with apply merge-with +