forked from cqframework/cqf-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercises04.cql
252 lines (169 loc) · 4.89 KB
/
Exercises04.cql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Intervals and Lists
Intervals
Comparing Intervals
Timing Phrases
List Values
List Operations
Aggregates
Lists of Strings
Lists of Lists
Lists of Intervals
Modify Each Expression so that it returns True, if it doesn't already
*/
library Exercises04
// # Intervals And Lists
define "Closed-Open Interval":
Interval[3, 5)
define "Closed-Open Decimal Interval":
Interval[3.0, 5.0)
define "Closed-Closed DateTime Interval":
Interval[@2014-01-01T00:00:00.0, @2015-01-01T00:00:00.0]
define "Interval Contains":
Interval[3, 5) contains 4
define "Interval In":
4 in Interval[3, 5)
define "Start Of Interval":
start of Interval[3, 5)
define "End of Interval":
end of Interval[3, 5)
define "Width of Interval":
width of Interval[3, 5)
define "Size of Interval":
Size(Interval[3, 5))
define "Interval Equality":
Interval[3, 5) = Interval[3, 4]
define "Interval Before":
Interval[1, 2] before Interval[3, 4]
define "Interval After":
Interval[3, 4] after Interval[1, 2]
define "Interval Meets":
Interval[1, 2] meets Interval[3, 4]
define "Interval Overlaps":
Interval[1, 5] overlaps Interval[3, 6]
define "Interval Starts":
Interval[1, 2] starts Interval[1, 5]
define "Interval Included In":
Interval[2, 4] included in Interval[1, 5]
define "Interval Includes":
Interval[1, 5] includes Interval[2, 4]
define "Interval Ends":
Interval[4, 5] ends Interval[1, 5]
// # Timing Phrases
define IntervalX:
Interval[@2021-03-01, @2021-03-10]
define IntervalY:
Interval[@2021-03-04, @2021-03-30]
define "Interval Starts Before Start":
IntervalX starts before start IntervalY
define "Interval Starts 3 Days Before Start":
IntervalX starts 3 days before start IntervalY
define "Interval Starts 3 Days Or More Before Start":
IntervalX starts 3 days or more before start IntervalY
define "Interval Starts Within 3 days of Start":
IntervalX starts within 3 days of start IntervalY
// # List Values
define "Integer List":
{ 1, 2, 3, 4, 5 }
define "Choice List":
{ 1, 'abc', null }
define "Tuple List":
{ { X: 1, Y: 1 }, { X: 2, Y: 2 } }
define "List Indexer":
{ 'a', 'b', 'c'}[1]
define "List Index Of":
IndexOf({ 1, 2, 3 }, 2)
define "List Contains":
{ 1, 'abc', null } contains null
define "List Length":
Length({ 1, 'abc', null })
define "List First":
First({ 1, 2, 3 })
define "List Last":
Last({ 1, 2, 3 })
define "Singleton From":
singleton from { 1 }
// This will throw a run-time error, stopping evaluation of the library
//define "Singleton From (Error)":
// singleton from { 1, 2, 3 }
define "List Take":
Take({ 1, 2, 3, 4 }, 2)
define "List Skip":
Skip({ 1, 2, 3, 4 }, 2)
define "List Tail":
Tail({ 1, 2, 3, 4 })
define "ListX":
{ 1, 2, 3, 4, 5 }
define "ListY":
{ 2, 3, 4 }
define "List Contains (2)":
ListX contains 3
define "List In (2)":
3 in ListX
define "List Includes":
ListX includes ListY
define "List Included In":
ListY included in ListX
// # List Union
define ListA: { 1, 2, 3, 4, 5 }
define ListB: { 4, 5, 6, 7, 8 }
define "List Union":
ListA union ListB
define "Choice List Union":
{ 1, 2, 3 } union { 'a', 'b', 'c' }
define "List Intersect":
ListA intersect ListB
define "List Except":
ListA except ListB
// # Aggregates
define "Aggregate Count":
Count({ 1, 2, 3, null})
define "Aggregate Sum":
Sum({ 1, 2, 3, null })
define "Aggregate Min":
Min({ 1, 2, 3, null })
define "Aggregate Max":
Max({ 1, 2, 3, null })
define "Aggregate Avg":
Avg({ 1, 2, 3, null })
define "Aggregate Median":
Median({ 1, 2, 3, null })
define "Aggregate Mode":
Mode({ 1, 2, 3, null })
define "Aggregate Variance":
Variance({ 1, 2, 3, null })
define "Aggregate StdDev":
StdDev({ 1, 2, 3, null })
define "Aggregate Population Variance":
PopulationVariance({ 1, 2, 3, null })
define "Aggregate Population StdDev":
PopulationStdDev({ 1, 2, 3, null })
define "Aggregate All True":
AllTrue({ true, false, true })
define "Aggregate Any True":
AnyTrue({ true, false, true })
// # Lists of Strings
define "Combine Expression":
Combine({ 'ab', 'cd', 'ef' })
define "Combine With Separator":
Combine({ 'completed', 'refused', 'pending' }, ';')
define "Split Expression":
Split('completed;refused;pending', ';')
// # Lists of Lists
define "Flatten Expression":
flatten { { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 } }
define "Distinct Expression":
distinct { 1, 2, 3, 2, 3, 4, 3, 4, 5 }
define "Flatten Not Recursive":
{ { 1, 2, 3 }, { { 'a', 'b' }, { 'x', 'y' } } }
define "Flatten Lists and Elements":
{ { 1, 2, 3 }, 2, 3, 4 }
// # Lists of Intervals
define "Collapse Expression":
collapse { Interval[1, 6], Interval[3, 7], Interval[9, 12] }
define "Expand Expression":
expand { Interval[1, 7], Interval[9, 12] }
// This overload of expand was introduced STU in 1.5 and is
// not yet implemented in the Java-based CQL engine
//define "Expand Interval Expression":
// expand Interval[1, 10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }