-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathExamples.cls
More file actions
346 lines (314 loc) · 10.2 KB
/
Copy pathExamples.cls
File metadata and controls
346 lines (314 loc) · 10.2 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/// examples for ObjectScript Tutorial
Class ObjectScript.Examples
{
/// demo of public and private methods, along with a public variable
ClassMethod PrivatePublic()
{
do ..Private() // call a private method
do ..Public(9) // call a public method
}
/// a private method with public variable a
ClassMethod Private() [ Private, PublicList = a ]
{
write !, "setting a" set a = 1
write !, "setting b" set b = 2
write !, "setting c" set c = 3
write !, "setting d" set d = 4
}
/// a public method with an argument and a return value
ClassMethod Public(num As %Numeric) As %String
{
write !, "my favorite number is ", num
return "This is my return value!!!"
}
/// demo of passing arguments by value and reference
ClassMethod PassingArguments(num As %Numeric)
{
// pass by value read !, "Enter a number: ", num
set dblnum = ..DoubleByVal(num)
write !, "By Value: ", num, " doubled is: ", dblnum
// num passed IN and OUT by reference
write !, "By Reference 1: ", num
do ..DoubleByRef1(.num)
write " doubled is: ", num
// num passed IN by value, result passed OUT by reference
do ..DoubleByRef2(num, .result)
write !, "By Reference 2: ", num, " doubled again is: ", result
}
ClassMethod DoubleByVal(anynumber As %Numeric) As %Numeric
{
return anynumber * 2
}
ClassMethod DoubleByRef1(ByRef anynumber As %Numeric)
{
set anynumber = anynumber * 2
}
ClassMethod DoubleByRef2(anynumber As %Numeric, Output retnumber As %Numeric)
{
set retnumber = anynumber * 2
}
/// demo of <UNDEFINED> error
ClassMethod BadMethod()
{
set a = 1
set b = 2
write c
}
/// root for my favorite team
ClassMethod Root()
{
read "Team: ", t
if (t = "") { quit } // stop execution if no team is specified
if (t = "METS") {
write !, "Go METS!" }
else {
write !, "Boo ", t, "!" }
}
/// demos of many Ifs
ClassMethod If()
{
set x = 5, y = 0, z = -5
if (x = 5) { write !, "x is equal to 5" } else { write !, "false" }
if (x = 10) { write !, "x is equal to 10" } else { write !, "false" }
if (x < y) { write !, "x is less than y" } else { write !, "false" }
if (x > y) { write !, "x is greater than y" } else { write !, "false" }
write !
if (##class(%SYSTEM.Util).NumberOfCPUs() > 2) { write !, "there are more than 2 CPUs" } else { write !, "false" }
if (x > $zsqr(64)) { write !, "x is greater than square root of 64" } else { write !, "false" }
write !
if (x && y) { write !, "both x and y are true (non-zero)" } else { write !, "false" }
if (x && z) { write !, "both x and z are true (non-zero)" } else { write !, "false" }
if (x && y && z) { write !, "x, y, and z are all true (non-zero)" } else { write !, "false" }
if (x || y || z) { write !, "at least one of x, y, or z is true (non-zero)" } else { write !, "false" }
write !
if ((x > y) || (y < z)) { write !, "either x is greater than y OR y is less than z" } else { write !, "false" }
if (x > y || y < z) { write !, "without proper parentheses, this expression is false" } else { write !, "false" }
if ((x > y) && (z < y)) { write !, "x is greater than y AND z is less than y" } else { write !, "false" }
if (x > y && z < y) { write !, "without proper parentheses, this expression is also false" } else { write !, "false" }
write !
if 'x { write !, "x is not true (zero)" } else { write !, "false" }
if 'y { write !, "y is not true (zero)" } else { write !, "false" }
if (x '< y) { write !, "x is not less than y" } else { write !, "false" }
if '(x < y) { write !, "x is not less than y" } else { write !, "false" }
}
ClassMethod Celebrate()
{
write !, "Yippee! I won!"
}
ClassMethod Complain()
{
write !, "Oh well, I lost."
}
/// demos of the For construct
ClassMethod For()
{
for i = 1:1:8 {
write !, "I ", i, " the sandbox."
}
write !!
for b = "John", "Paul", "George", "Ringo" {
write !, "Was ", b, " the leader? "
read yn
}
write !!
for i = 1:1 {
read !, "Capital of MA? ", a
if (a = "BOSTON") {
write "...did it in ", i, " tries"
quit
}
}
write !!
for i = 1:1 {
read !, "Capital of TX? ", a
continue:(a '= "AUSTIN")
write "...did it in ", i, " tries"
quit
}
write !!
for {
read !, "Know what? ", wh
quit:(wh = "NO!")
write " That's what!"
}
}
/// generate Fibonacci sequences
ClassMethod Fibonacci()
{
read !, "Generate Fibonacci sequence up to where? ", upto
set t1 = 1, t2 = 1, fib = 1
write !
do {
write fib, " "
set fib = t1 + t2, t1 = t2, t2 = fib
}
while (fib '> upto)
set t1 = 1, t2 = 1, fib = 1
write !
while (fib '> upto) {
write fib, " "
set fib = t1 + t2, t1 = t2, t2 = fib
}
}
/// examples of system and custom exceptions
ClassMethod Exceptions(x As %Numeric)
{
// <UNDEFINED> error throws a system exception
try {
write "Hello!", !, xyz
}
catch err {
write !, "Error name: ", ?20, err.Name,
!, "Error code: ", ?20, err.Code,
!, "Error location: ", ?20, err.Location,
!, "Additional data: ", ?20, err.Data, !
}
// <DIVIDE> error throws a system exception
try {
write 1/0
}
catch err {
write !, "Error name: ", ?20, err.Name,
!, "Error code: ", ?20, err.Code,
!, "Error location: ", ?20, err.Location,
!, "Additional data: ", ?20, err.Data, !
}
// create a simple custom exception object and throw it
set ex = ##class(%Exception.General).%New()
set ex.Name = "Demo Exception",
ex.Code = 100000,
ex.Data = "Tutorial Example"
try {
write !, "Hello!", !
if (x >= 5) throw ex // throw the exception
}
catch err {
write !, "Error name: ", ?20, err.Name,
!, "Error code: ", ?20, err.Code,
!, "Error location: ", ?20, err.Location,
!, "Additional data: ", ?20, err.Data, !
if (x = 5) return // terminate method
}
write !, "Finished!"
}
/// examples of JSON
ClassMethod JSON()
{
// create a JSON object
set jo1 = { "PartNum":"678LM", "Price":"7.99", "Quantity":"100" }
// create a JSON array, and add it to the object
set ar1 = ["Small","Large"], jo1.Sizes = ar1
// change a size in the 0-based JSON array
set ar1."0" = "Tiny"
// turn the JSON into a string and display it
set string1 = jo1.%ToJSON()
write !, "First JSON object: ", !, string1
// create a text string in JSON format
set string2 = "{""PartNum"":""345JK"", ""Price"":5.99, ""Sizes"":[""Small"", ""Medium"", ""Large""], ""Quantity"":50}"
// create an object from the string
set jo2 = ##class(%DynamicObject).%FromJSON(string2)
// display the properties of the object
write !!, "Second JSON Object:"
write !, "Part Number: ", jo2.PartNum, " Price: ", jo2.Price, " Quantity: ", jo2.Quantity
write !, "Sizes"
// loop through the array using an iterator
set ar2 = jo2.Sizes
set iter = ar2.%GetIterator()
while iter.%GetNext(.key , .value ) {
write !, ?5, "Key: ", key, ", Size: ", value
}
// change some of the properties
set jo2.Price = "8.99", jo2.Quantity = 75
// push a new size onto the end of the array
do ar2.%Push("Extra Large")
// turn the JSON into a string and display it
write !!, "Changed Second JSON Object:"
set newstring = jo2.%ToJSON()
write !, newstring
}
/// loop through last names of the ^PersonI global, 2 different ways
ClassMethod SimpleLoop()
{
write !, "Using argumentless For"
set ln = "" // initialize to the empty string to make $order return the first last name
for { // start looping
set ln = $order(^PersonI("Name", ln)) // use the current last name to get the next
quit:(ln = "") // stop looping when ln becomes empty again
write !, ?5, ln
}
write !!, "Using While"
set ln = $order(^PersonI("Name", "")) // get the first last name
while (ln '= "") { // only loop if there is at least one last name
write !, ?5, ln
set ln = $order(^PersonI("Name", ln)) // use the current last name to get the next
}
}
/// Loop through the name index and display the records
ClassMethod NameLoop()
{
// loop through last names
set ln = ""
for {
set ln = $order(^PersonI("Name", ln))
quit:(ln = "")
// for each last name, loop through first names
set fn = ""
for {
set fn = $order(^PersonI("Name", ln, fn))
quit:(fn = "")
// for each last name and first name, loop through id numbers
set id = ""
for {
set id = $order(^PersonI("Name", ln, fn, id))
quit:(id = "")
// once you have an id number, get the data and display it
set rec = ^PersonD(id)
write !, $list(rec, 1),
?15, $list(rec, 2),
?30, $zdate($list(rec, 3), 2)
}
}
}
}
/// loop through last names that FOLLOW a substring (including the substring)
ClassMethod FocusedLoopStart()
{
read "Search for: ",substring
// find the last name just BEFORE the substring and then start looping
set ln = $order(^PersonI("Name", substring), -1)
for {
set ln = $order(^PersonI("Name", ln))
quit:(ln = "")
write !, ln
}
}
/// loop through last names that MATCH substring
ClassMethod FocusedLoopStartEnd()
{
read "Search for: ",substring
// find the last name just BEFORE the substring and then start looping
set ln = $order(^PersonI("Name", substring), -1)
for {
set ln = $order(^PersonI("Name", ln))
// quit if no match or at end
quit:($extract(ln, 1, $length(substring)) '= substring)
write !, ln
}
}
/// examples of conversion methods
ClassMethod Conversions()
{
write !, "abcde becomes: ", $translate("abcde", "ad", "yz") // translate a->y, and d->z
write !, "abcde becomes: ", $translate("abcde", "ad", "zz") // translate a->z, and d->z
write !, "abcde becomes: ", $translate("abcde", "ad", "z") // translate a->z, and d->nothing
write !, "abcdebcbc becomes: ", $translate("abcdebcbc", "abc", "yz") // translate a->y, b->z, and c->nothing
write !, "abcdebcbc becomes: ", $replace("abcdebcbc", "abc", "yz") // replace abc->yz
read !, "String to translate: ", x
set lower = "abcdefghijklmnopqrstuvwxyz"
set upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
write !, "Using $translate: ", $translate(x, lower, upper)
write !, "Using $zconvert: ", $zconvert(x, "U")
write !, "Using $zconvert for capitalizing words: ", $zconvert(x, "W")
write !, "Using $zstrip to remove whitespace: ", $zstrip(x, "*W")
}
}