Skip to content

Commit 9fcfdc5

Browse files
committed
Release v1.2
2 parents 1cd7528 + 5021a28 commit 9fcfdc5

File tree

12 files changed

+1311
-0
lines changed

12 files changed

+1311
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Welcome to the DeclarativeCOS!
2+
## (available for Caché 2015.1+)
23
<br/>
34
<br/>
45
<br/>
@@ -22,6 +23,68 @@ The main point is not to add one-liners features to COS. The main point is to br
2223
Loop is just an instrument to solve the problem. In common, the problem is to traverse by collection and do some action with every element. Do you really need a loop for it? No. You choose a loop because COS supports loops only. DeclarativeCOS allows to write the code in declarative style. Just declare what you want to do and DeclarativeCOS will do all the rest for you.
2324
<br/>
2425
<br/>
26+
# OK. Examples, please.
27+
28+
**What is about output collection?**
29+
```
30+
/// Usual way
31+
32+
set i = collection.Next("")
33+
34+
while (i '= "") {
35+
set item = collection.GetAt(i)
36+
37+
w item,!
38+
39+
set i = collection.Next(i)
40+
}
41+
```
42+
```
43+
/// New way
44+
45+
d ##class(DeclarativeCOS.Binder).%New(collection, "io:println").ForEach()
46+
47+
/// or
48+
49+
zforeach $zbind(collection, "io:println")
50+
```
51+
52+
**What is about output collection? (one more)**
53+
```
54+
/// Usual way
55+
56+
set i = collection.Next("")
57+
58+
while (i '= "") {
59+
set item = collection.GetAt(i)
60+
61+
w item _ " "
62+
63+
set i = collection.Next(i)
64+
}
65+
```
66+
```
67+
/// New way
68+
69+
w ##class(DeclarativeCOS.Joiner).%New(collection, " ").join()
70+
71+
/// or
72+
73+
w $zjoin(collection, " ")
74+
```
75+
<br/>
76+
<br/>
77+
# How to install it?
78+
79+
In the root of the repo you can find two xml files:
80+
**install.base.xml**,
81+
**install.advanced.xml**.
82+
83+
The first file installs all needed classes to perform any examples above.
84+
85+
The second file installs short functions and commands: zforeach, $zmap, $zfind, $zfilter, $zexists, $zcount, $zjoin.
86+
<br/>
87+
<br/>
2588
# OK. How does it work?
2689

2790
**5 steps:**

install.advanced.xml

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Export generator="Cache" version="25" zv="Cache for UNIX (SUSE Linux Enterprise Server for x86-64) 2015.1 (Build 429U)" ts="2017-02-24 19:10:39">
3+
<Routine name="%ZLANGC00" type="MAC" languagemode="0" timestamp="64333,51194"><![CDATA[
4+
QUIT
5+
6+
//
7+
// "zforeach" command.
8+
//
9+
// Applies the certain function to each element of the collection.
10+
//
11+
// Example:
12+
// NAMESPACE> set collection = ##class(%ListOfDataTypes).%New()
13+
// NAMESPACE> do collection.Insert("Hello ")
14+
// NAMESPACE> do collection.Insert("World!")
15+
// NAMESPACE>
16+
// NAMESPACE> zforeach $zbind(collection, "io:print")
17+
//
18+
// See also:
19+
// DeclarativeCOS.Binder
20+
// DeclarativeCOS.Binder#ForEach
21+
// DeclarativeCOS.IO
22+
//
23+
ZFOREACH(binder) public {
24+
do binder.ForEach()
25+
Quit
26+
}
27+
]]></Routine>
28+
29+
30+
<Routine name="%ZLANGF00" type="MAC" languagemode="0" timestamp="64333,52314"><![CDATA[
31+
QUIT
32+
33+
//
34+
// "$zbind" function.
35+
//
36+
// Returns new instance of DeclarativeCOS.Binder with specified collection and function.
37+
//
38+
// Example:
39+
// NAMESPACE> set collection = ##class(%ListOfDataTypes).%New()
40+
// NAMESPACE> do collection.Insert("Hello ")
41+
// NAMESPACE> do collection.Insert("World!")
42+
// NAMESPACE>
43+
// NAMESPACE> zforeach $zbind(collection, "io:print")
44+
// NAMESPACE> Hello World!
45+
//
46+
// See also:
47+
// DeclarativeCOS.Binder
48+
// DeclarativeCOS.Binder#%New
49+
// DeclarativeCOS.Binder#ForEach
50+
// DeclarativeCOS.IO
51+
//
52+
ZBIND(collection, function) public {
53+
Quit ##class(DeclarativeCOS.Binder).%New(collection, function)
54+
}
55+
56+
//
57+
// "$zmap" function.
58+
//
59+
// Returns new collection where each item is result of apply the certain function to source item of the specified collection.
60+
//
61+
// Example:
62+
// NAMESPACE> set numbers = ##class(%ListOfDataTypes).%New()
63+
// NAMESPACE> do numbers.Insert($random(100))
64+
// NAMESPACE> do numbers.Insert($random(100))
65+
// NAMESPACE> do numbers.Insert($random(100))
66+
// NAMESPACE>
67+
// NAMESPACE> write "[" _ $zjoin(numbers, ",") _ "]"
68+
// NAMESPACE> [82,12,27]
69+
// NAMESPACE>
70+
// NAMESPACE> set hexNumbers = $zmap(numbers, "examples:toHex")
71+
// NAMESPACE>
72+
// NAMESPACE> for i=1:1:numbers.Count() { do numbers.SetAt($zhex(numbers.GetAt(i)), i) }
73+
// NAMESPACE>
74+
// NAMESPACE> write "[" _ $zjoin(numbers, ",") _ "]"
75+
// NAMESPACE> [52,C,1B]
76+
// NAMESPACE>
77+
// NAMESPACE> write $zjoin(hexNumbers, ",")
78+
// NAMESPACE> [52,C,1B]
79+
//
80+
// See also:
81+
// DeclarativeCOS.Binder#Map
82+
// DeclarativeCOS.Examples#toHex
83+
// DeclarativeCOS.Joiner#join ($zjoin)
84+
//
85+
ZMAP(collection, function) public {
86+
Quit $zbind(collection, function).Map()
87+
}
88+
89+
//
90+
// "$zfind" function.
91+
//
92+
// Returns the first found element from the specified collection by the certain criteria (function).
93+
// Otherwise, returns null string.
94+
//
95+
// Example:
96+
// NAMESPACE> set numbers = ##class(%ListOfDataTypes).%New()
97+
// NAMESPACE> do numbers.Insert($random(100))
98+
// NAMESPACE> do numbers.Insert($random(100))
99+
// NAMESPACE> do numbers.Insert($random(100))
100+
// NAMESPACE>
101+
// NAMESPACE> set primeNumber = $zfind(numbers, "examples:isPrime")
102+
// NAMESPACE>
103+
// NAMESPACE> write "[" _ $zjoin(numbers, ",") _ "]"
104+
// NAMESPACE> [69,41,68]
105+
// NAMESPACE>
106+
// NAMESPACE> write "Prime number: " _ $select(primeNumber="":"<not found>", 1:primeNumber)
107+
// NAMESPACE> Prime number: 41
108+
//
109+
// See also:
110+
// DeclarativeCOS.Binder#Find
111+
// DeclarativeCOS.Examples#isPrime
112+
// DeclarativeCOS.Joiner#join ($zjoin)
113+
//
114+
ZFIND(collection, function) public {
115+
Quit $zbind(collection, function).Find()
116+
}
117+
118+
//
119+
// "$zfilter" function.
120+
//
121+
// Returns new collection which contains filtered elements by the certain criteria (function) of the specified collection.
122+
//
123+
// Example:
124+
// NAMESPACE> set numbers = ##class(%ListOfDataTypes).%New()
125+
// NAMESPACE> do numbers.Insert($random(100))
126+
// NAMESPACE> do numbers.Insert($random(100))
127+
// NAMESPACE> do numbers.Insert($random(100))
128+
// NAMESPACE>
129+
// NAMESPACE> set filteredNumbers = $zfilter(numbers, "examples:isOdd")
130+
// NAMESPACE>
131+
// NAMESPACE> write "[" _ $zjoin(numbers, ",") _ "]"
132+
// NAMESPACE> [22,71,31]
133+
// NAMESPACE>
134+
// NAMESPACE> write "[" _ $zjoin(filteredNumbers, ",") _ "]"
135+
// NAMESPACE> [71,31]
136+
//
137+
// See also:
138+
// DeclarativeCOS.Binder#Filter
139+
// DeclarativeCOS.Examples#isOdd
140+
// DeclarativeCOS.Joiner#join ($zjoin)
141+
//
142+
ZFILTER(collection, function) public {
143+
Quit $zbind(collection, function).Filter()
144+
}
145+
146+
//
147+
// "$zexists" function.
148+
//
149+
// Returns $$$YES if collection contains element which is satisfied by by the certain criteria (function).
150+
// Otherwise, returns $$$NO.
151+
//
152+
// Example:
153+
// NAMESPACE> set numbers = ##class(%ListOfDataTypes).%New()
154+
// NAMESPACE> do numbers.Insert($random(100))
155+
// NAMESPACE> do numbers.Insert($random(100))
156+
// NAMESPACE> do numbers.Insert($random(100))
157+
// NAMESPACE>
158+
// NAMESPACE> set hasEvenNumbers = $zexists(numbers, "examples:isEven")
159+
// NAMESPACE>
160+
// NAMESPACE> write "[" _ $zjoin(numbers, ",") _ "]"
161+
// NAMESPACE> [51,56,53]
162+
// NAMESPACE>
163+
// NAMESPACE> write "Collection has" _ $case(hasEvenNumbers, 1:" ", 0:" no ") _ "even numbers"
164+
// NAMESPACE> Collection has even numbers
165+
//
166+
// See also:
167+
// DeclarativeCOS.Binder#Exists
168+
// DeclarativeCOS.Examples#isEven
169+
// DeclarativeCOS.Joiner#join ($zjoin)
170+
//
171+
ZEXISTS(collection, function) public {
172+
Quit $zbind(collection, function).Exists()
173+
}
174+
175+
//
176+
// "$zcount" function.
177+
//
178+
// Returns $$$YES if collection contains element which is satisfied by the certain criteria (function).
179+
// Otherwise, returns $$$NO.
180+
//
181+
// Example:
182+
// NAMESPACE> set numbers = ##class(%ListOfDataTypes).%New()
183+
// NAMESPACE> do numbers.Insert($random(1000))
184+
// NAMESPACE> do numbers.Insert($random(1000))
185+
// NAMESPACE> do numbers.Insert($random(1000))
186+
// NAMESPACE>
187+
// NAMESPACE> set palindromicNumbersCount = $zcount(numbers, "examples:isPalindromic")
188+
// NAMESPACE>
189+
// NAMESPACE> write "[" _ $zjoin(numbers, ",") _ "]"
190+
// NAMESPACE> [715,202,898]
191+
// NAMESPACE>
192+
// NAMESPACE> write "Count of palindromic numbers: " _ palindromicNumbersCount
193+
// NAMESPACE> Count of palindromic numbers: 2
194+
//
195+
// See also:
196+
// DeclarativeCOS.Binder#Count
197+
// DeclarativeCOS.Examples#isPalindromic
198+
// DeclarativeCOS.Joiner#join ($zjoin)
199+
//
200+
ZCOUNT(collection, function) public {
201+
Quit $zbind(collection, function).Count()
202+
}
203+
204+
//
205+
// "$zjoin" function.
206+
//
207+
// Returns string from elements of the specified collection usin the certain separator.
208+
//
209+
// Example:
210+
// NAMESPACE> set words = ##class(%ListOfDataTypes).%New()
211+
// NAMESPACE> do words.Insert("DeclarativeCOS")
212+
// NAMESPACE> do words.Insert("is")
213+
// NAMESPACE> do words.Insert("awesome!")
214+
// NAMESPACE>
215+
// NAMESPACE> write $jzoin(words, " ")
216+
// NAMESPACE> DeclarativeCOS is awesome!
217+
//
218+
// See also:
219+
// DeclarativeCOS.Joiner#join
220+
//
221+
ZJOIN(collection, separator = "") public {
222+
Quit ##class(DeclarativeCOS.Joiner).join(collection, separator)
223+
}
224+
]]></Routine>
225+
</Export>

0 commit comments

Comments
 (0)