Skip to content

Commit 1cd7528

Browse files
committed
Release v1.1
2 parents aed7d41 + 7145c4a commit 1cd7528

11 files changed

+480
-88
lines changed

src/cls/DeclarativeCOS.Binder.cls

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
/// Binder collection with function.
2+
/// Also, class provides implementation of all DeclarativeCOS commands and functions.
13
Class DeclarativeCOS.Binder Extends %RegisteredObject
24
{
35

6+
/// Source collection for processing in DeclarativeCOS commands and functions.
47
Property collection As %Collection.AbstractList [ Private ];
58

9+
/// Source function for processing in DeclarativeCOS commands and functions.
610
Property function As %String [ Private ];
711

12+
/// Overrides "constructor" for the class.
813
Method %OnNew(collection As %Collection.AbstractList, function As %String) As %Status [ Private, ServerOnly = 1 ]
914
{
1015
if ('##class(DeclarativeCOS.Utils).isValidName(function)) {
@@ -20,16 +25,34 @@ Method %OnNew(collection As %Collection.AbstractList, function As %String) As %S
2025
return $$$OK
2126
}
2227

28+
/// Returns source collection of the binder.
2329
Method GetCollection()
2430
{
2531
return ..collection
2632
}
2733

34+
/// Returns source function of the binder.
2835
Method GetFunction()
2936
{
3037
return ..function
3138
}
3239

40+
///
41+
/// Implementation of "zforeach" command.
42+
///
43+
/// Applies the certain function to each element of the collection.
44+
///
45+
/// Example:
46+
/// NAMESPACE> set collection = ##class(%ListOfDataTypes).%New()
47+
/// NAMESPACE> do collection.Insert("Hello ")
48+
/// NAMESPACE> do collection.Insert("World!")
49+
/// NAMESPACE>
50+
/// NAMESPACE> zforeach $zbind(collection, "io:print")
51+
///
52+
/// See also:
53+
/// DeclarativeCOS.Binder
54+
/// DeclarativeCOS.IO#print
55+
///
3356
Method ForEach()
3457
{
3558
quit:..collection=""
@@ -49,6 +72,34 @@ Method ForEach()
4972
}
5073
}
5174

75+
///
76+
/// Implementation of "$zmap" function.
77+
///
78+
/// Returns new collection where each item is result of apply the certain function to source item of the specified collection.
79+
///
80+
/// Example:
81+
/// NAMESPACE> set numbers = ##class(%ListOfDataTypes).%New()
82+
/// NAMESPACE> do numbers.Insert($random(100))
83+
/// NAMESPACE> do numbers.Insert($random(100))
84+
/// NAMESPACE> do numbers.Insert($random(100))
85+
/// NAMESPACE>
86+
/// NAMESPACE> write "[" _ $zjoin(numbers, ",") _ "]"
87+
/// NAMESPACE> [82,12,27]
88+
/// NAMESPACE>
89+
/// NAMESPACE> set hexNumbers = $zmap(numbers, "examples:toHex")
90+
/// NAMESPACE>
91+
/// NAMESPACE> for i=1:1:numbers.Count() { do numbers.SetAt($zhex(numbers.GetAt(i)), i) }
92+
/// NAMESPACE>
93+
/// NAMESPACE> write "[" _ $zjoin(numbers, ",") _ "]"
94+
/// NAMESPACE> [52,C,1B]
95+
/// NAMESPACE>
96+
/// NAMESPACE> write $zjoin(hexNumbers, ",")
97+
/// NAMESPACE> [52,C,1B]
98+
///
99+
/// See also:
100+
/// DeclarativeCOS.Examples#toHex
101+
/// DeclarativeCOS.Joiner#join ($zjoin)
102+
///
52103
Method Map()
53104
{
54105
set collection = $classmethod(..collection.%ClassName(), "%New")
@@ -72,6 +123,30 @@ Method Map()
72123
return collection
73124
}
74125

126+
///
127+
/// Implementation of "$zfind" function.
128+
///
129+
/// Returns the first found element from the specified collection by the certain criteria (function).
130+
/// Otherwise, returns null string.
131+
///
132+
/// Example:
133+
/// NAMESPACE> set numbers = ##class(%ListOfDataTypes).%New()
134+
/// NAMESPACE> do numbers.Insert($random(100))
135+
/// NAMESPACE> do numbers.Insert($random(100))
136+
/// NAMESPACE> do numbers.Insert($random(100))
137+
/// NAMESPACE>
138+
/// NAMESPACE> set primeNumber = $zfind(numbers, "examples:isPrime")
139+
/// NAMESPACE>
140+
/// NAMESPACE> write "[" _ $zjoin(numbers, ",") _ "]"
141+
/// NAMESPACE> [69,41,68]
142+
/// NAMESPACE>
143+
/// NAMESPACE> write "Prime number: " _ $select(primeNumber="":"<not found>", 1:primeNumber)
144+
/// NAMESPACE> Prime number: 41
145+
///
146+
/// See also:
147+
/// DeclarativeCOS.Examples#isPrime
148+
/// DeclarativeCOS.Joiner#join ($zjoin)
149+
///
75150
Method Find()
76151
{
77152
set index = ""
@@ -93,6 +168,29 @@ Method Find()
93168
return ""
94169
}
95170

171+
///
172+
/// Implementation of "$zfilter" function.
173+
///
174+
/// Returns new collection which contains filtered elements by the certain criteria (function) of the specified collection.
175+
///
176+
/// Example:
177+
/// NAMESPACE> set numbers = ##class(%ListOfDataTypes).%New()
178+
/// NAMESPACE> do numbers.Insert($random(100))
179+
/// NAMESPACE> do numbers.Insert($random(100))
180+
/// NAMESPACE> do numbers.Insert($random(100))
181+
/// NAMESPACE>
182+
/// NAMESPACE> set filteredNumbers = $zfilter(numbers, "examples:isOdd")
183+
/// NAMESPACE>
184+
/// NAMESPACE> write "[" _ $zjoin(numbers, ",") _ "]"
185+
/// NAMESPACE> [22,71,31]
186+
/// NAMESPACE>
187+
/// NAMESPACE> write "[" _ $zjoin(filteredNumbers, ",") _ "]"
188+
/// NAMESPACE> [71,31]
189+
///
190+
/// See also:
191+
/// DeclarativeCOS.Examples#isOdd
192+
/// DeclarativeCOS.Joiner#join ($zjoin)
193+
///
96194
Method Filter()
97195
{
98196
set collection = $classmethod(..collection.%ClassName(), "%New")
@@ -116,11 +214,59 @@ Method Filter()
116214
return collection
117215
}
118216

217+
///
218+
/// Implementation of "$zexists" function.
219+
///
220+
/// Returns $$$YES if collection contains element which is satisfied by by the certain criteria (function).
221+
/// Otherwise, returns $$$NO.
222+
///
223+
/// Example:
224+
/// NAMESPACE> set numbers = ##class(%ListOfDataTypes).%New()
225+
/// NAMESPACE> do numbers.Insert($random(100))
226+
/// NAMESPACE> do numbers.Insert($random(100))
227+
/// NAMESPACE> do numbers.Insert($random(100))
228+
/// NAMESPACE>
229+
/// NAMESPACE> set hasEvenNumbers = $zexists(numbers, "examples:isEven")
230+
/// NAMESPACE>
231+
/// NAMESPACE> write "[" _ $zjoin(numbers, ",") _ "]"
232+
/// NAMESPACE> [51,56,53]
233+
/// NAMESPACE>
234+
/// NAMESPACE> write "Collection has" _ $case(hasEvenNumbers, 1:" ", 0:" no ") _ "even numbers"
235+
/// NAMESPACE> Collection has even numbers
236+
///
237+
/// See also:
238+
/// DeclarativeCOS.Examples#isEven
239+
/// DeclarativeCOS.Joiner#join ($zjoin)
240+
///
119241
Method Exists()
120242
{
121243
return ..Find() '= ""
122244
}
123245

246+
///
247+
/// Implementation of "$zcount" function.
248+
///
249+
/// Returns $$$YES if collection contains element which is satisfied by the certain criteria (function).
250+
/// Otherwise, returns $$$NO.
251+
///
252+
/// Example:
253+
/// NAMESPACE> set numbers = ##class(%ListOfDataTypes).%New()
254+
/// NAMESPACE> do numbers.Insert($random(1000))
255+
/// NAMESPACE> do numbers.Insert($random(1000))
256+
/// NAMESPACE> do numbers.Insert($random(1000))
257+
/// NAMESPACE>
258+
/// NAMESPACE> set palindromicNumbersCount = $zcount(numbers, "examples:isPalindromic")
259+
/// NAMESPACE>
260+
/// NAMESPACE> write "[" _ $zjoin(numbers, ",") _ "]"
261+
/// NAMESPACE> [715,202,898]
262+
/// NAMESPACE>
263+
/// NAMESPACE> write "Count of palindromic numbers: " _ palindromicNumbersCount
264+
/// NAMESPACE> Count of palindromic numbers: 2
265+
///
266+
/// See also:
267+
/// DeclarativeCOS.Examples#isPalindromic
268+
/// DeclarativeCOS.Joiner#join ($zjoin)
269+
///
124270
Method Count()
125271
{
126272
set count = 0
@@ -144,7 +290,12 @@ Method Count()
144290
return count
145291
}
146292

147-
ClassMethod loadDeclarative(function As %String)
293+
/// Delegates call to DeclarativesManager#loadDeclarative
294+
///
295+
/// See also:
296+
/// DeclarativeCOS.DeclarativesManager#loadDeclarative
297+
///
298+
ClassMethod loadDeclarative(function As %String) [ Private ]
148299
{
149300
return ##class(DeclarativeCOS.DeclarativesManager).loadDeclarative(function)
150301
}

src/cls/DeclarativeCOS.DeclarativeProvider.cls

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
/// The heart of DeclarativeCOS!
2+
/// All declarative providers must extend the class.
13
Class DeclarativeCOS.DeclarativeProvider
24
{
35

6+
/// The DeclarativeCOS magic.
7+
/// Code generator for registration declaratives in
8+
/// special global: ^DeclarativeCOS
49
ClassMethod register() [ CodeMode = objectgenerator ]
510
{
611
if (%compiledclass.Name = "DeclarativeCOS.DeclarativeProvider") {

src/cls/DeclarativeCOS.DeclarativesManager.cls

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
/// Manager for declaratives.
2+
/// The class contains methods for saving and retrieving declaratives from ^DeclarativeCOS global.
13
Class DeclarativeCOS.DeclarativesManager
24
{
35

6+
/// Stores declarative to the ^DeclarativeCOS global.
47
ClassMethod saveDeclarative(declarativeName As %String, className As %String, functionName As %String)
58
{
69
set ^DeclarativeCOS("functions", declarativeName) = $lb(className, functionName)
710
}
811

12+
/// Loads declarative from the ^DeclarativeCOS global.
913
ClassMethod loadDeclarative(declarativeName As %String)
1014
{
1115
return $get(^DeclarativeCOS("functions", declarativeName))
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// Set of DeclarativeCOS examples.
2+
Class DeclarativeCOS.Examples Extends DeclarativeCOS.DeclarativeProvider
3+
{
4+
5+
/// Returns hex value of the passed value.
6+
///
7+
/// @Declarative("examples:toHex")
8+
ClassMethod toHex(value As %Numeric)
9+
{
10+
return $zhex(value)
11+
}
12+
13+
/// Returns $$$YES if passed value is prime number.
14+
/// Otherwise, returns $$$NO.
15+
///
16+
/// @Declarative("examples:isPrime")
17+
ClassMethod isPrime(value As %Numeric)
18+
{
19+
for i=2:1:$zsqr(value) {
20+
if (value # i = 0) {
21+
return $$$NO
22+
}
23+
}
24+
25+
return $$$YES
26+
}
27+
28+
/// Returns $$$YES if passed value is odd number.
29+
/// Otherwise, returns $$$NO.
30+
///
31+
/// @Declarative("examples:isOdd")
32+
ClassMethod isOdd(value As %Numeric)
33+
{
34+
return value # 2 '= 0
35+
}
36+
37+
/// Returns $$$YES if passed value is even number.
38+
/// Otherwise, returns $$$NO.
39+
///
40+
/// @Declarative("examples:isEven")
41+
ClassMethod isEven(value As %Numeric)
42+
{
43+
return value # 2 = 0
44+
}
45+
46+
/// Returns $$$YES if passed value is palindromic number.
47+
/// Otherwise, returns $$$NO.
48+
///
49+
/// @Declarative("examples:isPalindromic")
50+
ClassMethod isPalindromic(value As %Numeric)
51+
{
52+
return $reverse(value) = value
53+
}
54+
55+
}
56+

src/cls/DeclarativeCOS.IO.cls

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
/// The class provides IO declaratives.
2+
/// io:print <-> w value
3+
/// io:println <-> w value, !
14
Class DeclarativeCOS.IO Extends DeclarativeCOS.DeclarativeProvider
25
{
36

7+
/// Output passed value.
8+
///
49
/// @Declarative("io:print")
510
ClassMethod print(value As %Library.DataType)
611
{
712
w value
813
}
914

15+
/// Output passed value. Add newline symbol.
16+
///
1017
/// @Declarative("io:println")
1118
ClassMethod println(value As %Library.DataType)
1219
{

src/cls/DeclarativeCOS.Joiner.cls

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
/// Joiner is a kind of text utils class.
2+
/// The main point of the class is join elemenet of collection using separator.
13
Class DeclarativeCOS.Joiner
24
{
35

6+
/// Returns string from elements of the specified collection usin the certain separator.
7+
/// Example:
8+
/// NAMESPACE> set words = ##class(%ListOfDataTypes).%New()
9+
/// NAMESPACE> do words.Insert("DeclarativeCOS")
10+
/// NAMESPACE> do words.Insert("is")
11+
/// NAMESPACE> do words.Insert("awesome!")
12+
/// NAMESPACE>
13+
/// NAMESPACE> write $jzoin(words, " ")
14+
/// NAMESPACE> DeclarativeCOS is awesome!
415
ClassMethod join(collection As %Collection.AbstractList, separator As %String = "") As %String
516
{
617
if (collection = "") {

src/cls/DeclarativeCOS.Utils.cls

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
/// Some utils for DeclarativeCOS implementation.
12
Class DeclarativeCOS.Utils
23
{
34

5+
/// Declarative name regular expression.
6+
/// The pattern is "namespace:function"
47
Parameter DECLARATIVENAMEREGEX [ Internal ] = "[a-zA-Z0-9]+(.[a-zA-Z0-9]+)*\s*:\s*[a-zA-Z0-9]+([a-zA-Z0-9_]+)*";
58

9+
/// Returns $$$YES if specified name is satisfied by the DECLARATIVENAMEREGEX regex.
10+
/// Otherwise, returns $$$NO.
611
ClassMethod isValidName(name As %String) As %Boolean
712
{
813
return ##class(%Regex.Matcher).%New("^" _ ..#DECLARATIVENAMEREGEX _ "$", name).Locate()
914
}
1015

16+
/// Returns declarative name without any space characters.
1117
ClassMethod normalizeName(name As %String) As %String
1218
{
1319
if (..isValidName(name)) {

src/mac/%ZLANGC00.ro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
QUIT
22
.
3+
//
4+
// "zforeach" command.
5+
//
6+
// Applies the certain function to each element of the collection.
7+
//
8+
// Example:
9+
// NAMESPACE> set collection = ##class(%ListOfDataTypes).%New()
10+
// NAMESPACE> do collection.Insert("Hello ")
11+
// NAMESPACE> do collection.Insert("World!")
12+
// NAMESPACE>
13+
// NAMESPACE> zforeach $zbind(collection, "io:print")
14+
//
15+
// See also:
16+
// DeclarativeCOS.Binder
17+
// DeclarativeCOS.Binder#ForEach
18+
// DeclarativeCOS.IO
19+
//
320
ZFOREACH(binder) public {
421
do binder.ForEach()
522
Quit

0 commit comments

Comments
 (0)