corrade-nucleus-nucleons – Blame information for rev 20

Subversion Repositories:
Rev:
Rev Author Line No. Line
20 office 1 vars it, p
2  
3 p = {label, value|
4 print("\n" + label)
5 print(inspect(value))
6 }
7 -- Create an array from 0 to 15
8 p("range", i-collect(range(5)))
9  
10 -- Create an array from 0 to 15 and break up in chunks of 4
11 p("chunked range", i-collect(i-chunk(4, range(16))))
12  
13 -- Check if all or none items in stream pass test.
14 p("all < 60 in range(60)", i-all?({i|i<60}, range(60)))
15 p("any < 60 in range(60)", i-any?({i|i>60}, range(60)))
16 p("all < 60 in range(70)", i-all?({i|i<60}, range(70)))
17 p("any < 60 in range(70)", i-any?({i|i>60}, range(70)))
18  
19 -- Zip three different collections together
20 p("zipped", i-collect(i-zip(
21 range(10),
22 [1,2,3,4,5],
23 i-map({i|i*i}, range(10))
24 )))
25  
26 vars names, person, i, doubles, lengths, cubeRange
27 names = ["Thorin", "Dwalin", "Balin", "Bifur", "Bofur", "Bombur", "Oin",
28 "Gloin", "Ori", "Nori", "Dori", "Fili", "Kili", "Bilbo", "Gandalf"]
29  
30 for name in names {
31 if name != "Bilbo" && name != "Gandalf" {
32 print(name)
33 }
34 }
35  
36 person = {name: "Tim", age: 30}
37 for key, value in person {
38 print(key + " = " + value)
39 }
40  
41 i = 0
42 while i < 10 {
43 i = i + 1
44 print(i)
45 }
46  
47 print("range")
48 for i in range(10) {
49 print(i + 1)
50 }
51 for i in range(10) {
52 print(10 - i)
53 }
54  
55 -- Dynamic object that gives the first 10 doubles
56 doubles = {
57 @len: {| 10 }
58 @get: {key|
59 if key is Integer { key * key }
60 }
61 }
62 print("#doubles", #doubles)
63  
64 print("Doubles")
65 for k, v in doubles {
66 print([k, v])
67 }
68  
69 -- Dynamic object that has names list as keys and string lenth as values
70 lengths = {
71 @keys: {| names }
72 @get: {key|
73 if key is String { #key }
74 }
75 }
76  
77 print ("Lengths")
78 for k, v in lengths {
79 print([k, v])
80 }
81  
82  
83 cubeRange = {n|
84 vars i, v
85 i = 0
86 {
87 @call: {|
88 v = i
89 i = i + 1
90 if v < n { v * v * v }
91 }
92 }
93 }
94  
95 print("Cubes")
96 for k, v in cubeRange(5) {
97 print([k, v])
98 }
99 print("String")
100 for k, v in "Hello World" {
101 print([k, v])
102 }
103  
104  
105 print([i for i in range(10)])
106 print([i for i in range(20) if i % 3])
107  
108  
109  
110 -- Example showing how to do parallel work using split..and
111 base = {bootstrap, target-dir|
112 split {
113 copy("res", target-dir)
114 } and {
115 if newer("src/*.less", target-dir + "/style.css") {
116 lessc("src/" + bootstrap + ".less", target-dir + "/style.css")
117 }
118 } and {
119 build("src/" + bootstrap + ".js", target-dir + "/app.js")
120 }
121 }
122  
123  
124 vars Dragon, pet
125  
126 Dragon = {name|
127 vars asleep, stuff-in-belly, stuff-in-intestine,
128 feed, walk, put-to-bed, toss, rock,
129 hungry?, poopy?, passage-of-time
130  
131 asleep = false
132 stuff-in-belly = 10 -- He's full.
133 stuff-in-intestine = 0 -- He doesn't need to go.
134  
135 print(name + ' is born.')
136  
137 feed = {|
138 print('You feed ' + name + '.')
139 stuff-in-belly = 10
140 passage-of-time()
141 }
142  
143 walk = {|
144 print('You walk ' + name + ".")
145 stuff-in-intestine = 0
146 passage-of-time
147 }
148  
149 put-to-bed = {|
150 print('You put ' + name + ' to bed.')
151 asleep = true
152 for i in range(3) {
153 if asleep {
154 passage-of-time()
155 }
156 if asleep {
157 print(name + ' snores, filling the room with smoke.')
158 }
159 }
160 if asleep {
161 asleep = false
162 print(name + ' wakes up slowly.')
163 }
164 }
165  
166 toss = {|
167 print('You toss ' + name + ' up into the air.')
168 print('He giggles, which singes your eyebrows.')
169 passage-of-time()
170 }
171  
172 rock = {|
173 print('You rock ' + name + ' gently.')
174 asleep = true
175 print('He briefly dozes off...')
176 passage-of-time()
177 if asleep {
178 asleep = false
179 print('...but wakes when you stop.')
180 }
181 }
182  
183 hungry? = {|
184 stuff-in-belly <= 2
185 }
186  
187 poopy? = {|
188 stuff-in-intestine >= 8
189 }
190  
191 passage-of-time = {|
192 if stuff-in-belly > 0 {
193 -- Move food from belly to intestine
194 stuff-in-belly = stuff-in-belly - 1
195 stuff-in-intestine = stuff-in-intestine + 1
196 } else { -- Our dragon is starving!
197 if asleep {
198 asleep = false
199 print('He wakes up suddenly!')
200 }
201 print(name + ' is starving! In desperation, he ate YOU!')
202 abort "died"
203 }
204  
205 if stuff-in-intestine >= 10 {
206 stuff-in-intestine = 0
207 print('Whoops! ' + name + ' had an accident...')
208 }
209  
210 if hungry?() {
211 if asleep {
212 asleep = false
213 print('He wakes up suddenly!')
214 }
215 print(name + "'s stomach grumbles...")
216 }
217  
218 if poopy?() {
219 if asleep {
220 asleep = false
221 print('He wakes up suddenly!')
222 }
223 print(name + ' does the potty dance...')
224 }
225 }
226  
227 -- Export the public interface to this closure object.
228 {
229 feed: feed
230 walk: walk
231 put-to-bed: put-to-bed
232 toss: toss
233 rock: rock
234 }
235  
236 }
237  
238 pet = Dragon('Norbert')
239 pet.feed()
240 pet.toss()
241 pet.walk()
242 pet.put-to-bed()
243 pet.rock()
244 pet.put-to-bed()
245 pet.put-to-bed()
246 pet.put-to-bed()
247 pet.put-to-bed()