1+ /// Binder collection with function.
2+ /// Also, class provides implementation of all DeclarativeCOS commands and functions.
13Class DeclarativeCOS .Binder Extends %RegisteredObject
24{
35
6+ /// Source collection for processing in DeclarativeCOS commands and functions.
47Property collection As %Collection .AbstractList [ Private ];
58
9+ /// Source function for processing in DeclarativeCOS commands and functions.
610Property function As %String [ Private ];
711
12+ /// Overrides "constructor" for the class.
813Method %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.
2329Method GetCollection ()
2430{
2531 return ..collection
2632}
2733
34+ /// Returns source function of the binder.
2835Method 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+ ///
3356Method 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+ ///
52103Method 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+ ///
75150Method 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+ ///
96194Method 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+ ///
119241Method 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+ ///
124270Method 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}
0 commit comments