I’m trying to match on some data and a bit confused as to how to approach it. The structure looks like this
{:key-1 [{:name "name"
:amount "amount"
{:sub-key-1 [{:name "name"
:amount "amount"}
..(keeps repeating)..]}
This is part of a larger structure which is using m/match
.
Currently I am telling Meander.
{:key-1 [{:name !name
:amount !amount} ...]}
But this returns [!name …]
and [!amount …]
as separate lists.
My best strategy was to just pass this into another function that performs a search
What do you want to do with that data? What output are you looking for?
@jimmy I was trying to extract data from a vector of maps. Keeping the data of each map associated with one another.
I ended up achieving the desired result by calling another function that performed a
(m/search param
(m/scan {...}))
This is called on the logic variable (which I bound the entire vector structure to) inside the action of my top level m/match
If you share your full code, we could talk about other ways you could accomplish the same thing. If you can't I understand
I’ll try that later. Will have to remove the sensitive information from it.
Even just giving an example of "this is my input and the output should be _" will help. But I'm glad you got something working :slightly_smiling_face:
“aligned memory variables” is a common question; if the arrays are strongly aligned you can just substitute, if they are ragged you can use ..!n
to ensure the correct numbers are gathered, and you can also use a defsyntax
or with
sub pattern… more concrete examples for the cookbook would be useful so as you run into use cases it would be really helpful to share them in some anonymous cut down form.
@jimmy this is a Gist of a cut down version of what I have. https://gist.github.com/groundedSAGE/9dc0dd20113e29109dacdc08ba5dd66a
I’m interested if there is a way to avoid the sales-category-transform
function.
@timothypratley I’m sure there is a way to do what I am wanting to do as Meander seems to cover a lot of ground. I’m very new to it though. Only looked at it last night and used it at work today.
(m/rewrite data
{:Data
{:EventId ?event-id
:Event ?event-name
:Sales {:Overall {:TicketCount ?sales-ticket-count
:Amount ?sales-amount}
:ByCategory [{:Name !names
:TicketCount !ticket-counts
:Amount !amounts} ...]}}}
{:event {:id ?event-id
:name ?event-name}
:sales {:overall {:ticket-count ?sales-ticket-count
:amount ?sales-amount}
:by-category [{:name !names
:ticket-count !ticket-counts
:amount !amounts} ...]}})
Here's how you can do that same thing using rewrite.With rewrite, the right hand side is what is called a substitution. So you can do things like ...
and get the memory variables to work how you want.
That did it! @jimmy. Though I’m unsure which is the better choice as I know very little about this rewrite thing.
I do think it is good having a full representation of input/output data instead of breaking it into other functions.
Rewrite is match. But on the right hand side you can use substitute. Rewrite is definitely the better option here.
Cool. Well thank you for the help 🙂
Will be using this heavily at work so I’ll be around here a bit 😉