Stream: cql
Topic: Maintaining State During Iteration
Pascal Brandt (Apr 14 2021 at 17:44):
@Bryn Rhodes, or anyone, is there any good way to maintain state during iteration? In my case, I'm trying to calculate a running total over a list of non-unique values. So, I want to turn something that looks kind of like:
L = { 1, 2, 2, 3, 4, 5 }
Into something that looks like:
T = { 1, 3, 5, 8, 12, 17 }
My elements are actually Tuple
s with a date and a value, but the above example illustrates the problem.
At first I tried recursion, but it seems like that isn't supported. Then I thought I could use IndexOf
, Take
, and Sum
, but my elements are not unique. Is there a way to do this that I'm not thinking of? I'm using cql-to-elm
to generate my ELM, and cql-execution
to run it.
Chris Moesel (Apr 14 2021 at 18:00):
Hi @Pascal Brandt! Are you able to use the soon-to-be-published CQL 1.5 version? If so, you might want to take a look at Aggregate Queries. If I understand what you're trying to do correctly, I think this would provide a pretty straight-forward approach.
The CQL-to-ELM 1.5.2 translator supports it already and we've also added support in cql-execution
(thanks to @Rob Dingwell) -- but note that it is not yet released in cql-execution
. We can probably cut a new release of cql-execution
soon if you think this will be helpful.
Pascal Brandt (Apr 14 2021 at 18:07):
I'm using all kinds of custom builds, so I don't think pulling in new features is going to be a problem. I'm already on 1.5.2
of cql-to-elm
. I'm using the CQL Testing Framework for execution, but I'm on a custom build of that as well, so let me update cql-execution
there. Any gitsha you can recommend?
Thanks @Chris Moesel!
Chris Moesel (Apr 14 2021 at 18:47):
I haven't tried loading the cql-execution
package using a reference to it on GH. Hopefully that works OK. You can use the HEAD of master right now, which is sha 160b69e29a2d88e0891276986a551661772df4b5
.
Pascal Brandt (Apr 14 2021 at 20:04):
Just as a follow-up, everything seems to be working as expected. Here are the steps I followed in case anyone wants to do the same.
First I added a resolution in my package.json
to grab the gitsha I wanted:
"resolutions": {
"cql-execution": "cqframework/cql-execution#160b69e"
}
Then, I had to add a postinstall
script to build the (required) lib
directory:
"scripts": {
"postinstall": "yarn --cwd ./node_modules/cql-execution install"
}
Chris Moesel (Apr 14 2021 at 20:16):
Ah, yes. It was that postinstall
step that had me concerned. Thanks for sharing. And... let us know how the aggregate query approach works for you!
Pascal Brandt (Apr 14 2021 at 22:02):
It worked perfectly! Here's my expression:
define "Sorted Dates With Totals":
(Tail("Sorted Dates With Counts")) DC
aggregate all DT starting ({
Tuple {
date: First("Sorted Dates With Counts").date,
total: First("Sorted Dates With Counts").count
}
}): flatten {
DT,
Tuple {
date: DC.date,
total: Last(DT).total + DC.count
}
}
"Sorted Dates With Totals"
is of type List<Tuple { date System.DateTime, count Integer }>
. The only thing that tripped me up initially is the default to set semantics. Adding all
made sure duplicates weren't ignored.
Last updated: Apr 12 2022 at 19:14 UTC