meander

Anton Shastun 2024-09-04T16:34:17.877579Z

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

markaddleman 2024-09-04T16:37:14.961929Z

Not really. You can use m/app as an escape hatch to reduce / aggregate data.

markaddleman 2024-09-04T16:37:52.053839Z

I hear meander zeta will be able to handle this situation (friendly poke @noprompt)

🙂 1
Anton Shastun 2024-09-04T16:38:44.533339Z

but i have no idea how to do that even with m/app

markaddleman 2024-09-04T16:39:52.498269Z

Something like this:

(m/rewrites data                                                                                                                                        
     (m/$ {:connections (m/some {:active !a :created !c})})                                                                                                
     {:active (m/app (fn[as] (reduce + as)) [!a ...])}))

markaddleman 2024-09-04T16:40:36.282469Z

Notice the use of ! instead of ?

markaddleman 2024-09-04T16:41:40.535749Z

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

markaddleman 2024-09-04T16:43:13.572219Z

Does that make sense?

Anton Shastun 2024-09-04T16:45:28.589369Z

@markaddleman thank you, for explanation!

Anton Shastun 2024-09-04T16:45:53.274359Z

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

Anton Shastun 2024-09-04T16:46:30.451219Z

it returns array of maps instead of single map

markaddleman 2024-09-04T16:47:06.682939Z

I think you want to use m/rewrite instead of m/rewrites

Anton Shastun 2024-09-04T16:47:49.638169Z

but i need to aggregate all {:active !a :created !b}

Anton Shastun 2024-09-04T16:48:06.516289Z

inside that data structure

markaddleman 2024-09-04T16:48:43.211289Z

Thank you for explaining.

markaddleman 2024-09-04T16:49:08.085939Z

The m/app sum… operation is applied at each match.

markaddleman 2024-09-04T16:49:32.427319Z

You want to sum over all matches. Is that correct?

Anton Shastun 2024-09-04T16:49:59.131009Z

that way it works as expected:

#(apply merge-with +                                                                                                                                            
     (m/rewrites %                                                                                                                                           
          (m/$ {:connections (m/some {:active ?a :created ?c})})                                                                                                
          {:active ?a :created ?c}))

Anton Shastun 2024-09-04T16:50:13.754369Z

"You want to sum over all matches. Is that correct?" yes

markaddleman 2024-09-04T16:50:19.324649Z

Yes, that makes sense. I apologize - I assumed a different datastructure

Anton Shastun 2024-09-04T16:50:40.522229Z

np )

markaddleman 2024-09-04T16:52:24.956829Z

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

Anton Shastun 2024-09-04T17:09:34.571489Z

thats clear, thanks! then i will go with apply merge-with +

👍 1