fork download
  1. importPackage(java.io);
  2. importPackage(java.lang);
  3.  
  4. //TODO: comment
  5. function getFirstNumberOneBased() {
  6. return 1;
  7. }
  8.  
  9. /*
  10. This function expects following arguments:
  11. a - some integer number
  12. b - some integer number
  13. It returns arithmetical sum of this two numbers. Note that there are minimum and maximum bound of integers which are representable as JS integers, so this operation may cause overflow.
  14. */
  15. function sum(a, b) {
  16. //TODO: check if overflow may take place and throw exception
  17. return a + b;
  18. }
  19.  
  20. /*
  21. This function expects following arguments:
  22. array - instance of Array
  23. It returns magic number representing index of first index in array if array isn't empty.
  24. Calling this function with empty array as an argument leads to undefined behaviour!
  25. */
  26. function getArrayFromIndex(array) {
  27. return adjustFromOneBasedToZeroBasedNumber(getFirstNumberOneBased());
  28. }
  29.  
  30. /*
  31. This function expects following arguments:
  32. array - instance of Array
  33. It returns number of items in array.
  34. */
  35. function getArrayLength(array) {
  36. return array.length;
  37. }
  38.  
  39. /*
  40. This function expects following arguments:
  41. array - instance of Array
  42. It returns number representing index of last index in array unless array is empty.
  43. Calling this function with empty array as an argument leads to undefined behaviour!
  44. */
  45. function getArrayToIndex(array) {
  46. return adjustFromOneBasedToZeroBasedNumber(getArrayLength(array));
  47. }
  48.  
  49. /*
  50. This function expects following arguments:
  51. cond - some boolean value
  52. first - some arbitrary value
  53. second - some arbitrary value
  54. It returns first if "cond" evaluates to boolean value "true", it returns second if "cond" evaluates to boolean value "false". Behaviour isn't defined for other cases.
  55. */
  56. function conditionalValue(cond, first, second) {
  57. if (cond) {
  58. return first;
  59. }
  60. if (!cond) {
  61. return second;
  62. }
  63. }
  64.  
  65. /*
  66. This function expects following arguments:
  67. from - some number
  68. isInclusiveFrom - boolean value, which indicate whether "from" value is part of the range or not
  69. to - some number
  70. isInclusiveTo - boolean value, which indicate whether "to" value is part of the range or not
  71. It returns function which accepts single numeric argument and return true if it belongs to specified range and false otherwise.
  72. */
  73. function inRangeCondition(from, isInclusiveFrom, to, isInclusiveTo) {
  74. return function(value) {
  75. return conditionalValue(((from > value) || (value > to) || ((value == from) && (!isInclusiveFrom)) || ((value == to) && (!isInclusiveTo))), false, true);
  76. }
  77. }
  78.  
  79. /*
  80. This function expects following arguments:
  81. val - some integer number, which should be less than maximum integer value supported by platform.
  82. It returns val incremented by 1.
  83. */
  84. function incrementStep(val) {
  85. return sum(val, 1);
  86. }
  87.  
  88. //TODO: comment
  89. function genericIterate(from, continueCondition, step, func) {
  90. //Unfortunately tails calls aren't optimized in JS :(
  91. /*if (continueCondition(from)) {
  92. func(from);
  93. genericIterate(step(from), continueCondition, step, func);
  94. }*/
  95. var value = from;
  96. while (continueCondition(value)) { //TODO: use conditionalEvaluate function instead of "while" hack
  97. func(value);
  98. value = step(value);
  99. }
  100. }
  101.  
  102. //TODO: comment
  103. function rangeIterate(from, to, inclusiveTo, func) { //TODO: do we need inclusiveFrom argument?
  104. var cond = inRangeCondition(from, true, to, inclusiveTo); //TODO: explain magic "true" value
  105. genericIterate(from, cond, incrementStep, func);
  106. }
  107.  
  108. //TODO: comment
  109. function arrayIngicesIterate(array, func) {
  110. rangeIterate(getArrayFromIndex(array), getArrayToIndex(array), true, func); //TODO: explain magic "true" value
  111. }
  112.  
  113. //TODO: comment
  114. function getArrayItemByIndex(array, index) {
  115. return array[index];
  116. }
  117.  
  118. //TODO: comment
  119. function arrayValuesIterate(array, func) {
  120. arrayIngicesIterate(array, function(index) {
  121. var arrayItemValue = getArrayItemByIndex(array, index)
  122. func(arrayItemValue);
  123. });
  124. }
  125.  
  126. //TODO: comment
  127. function createArray() {
  128. return new Array();
  129. }
  130.  
  131. //TODO: comment
  132. function addItemToTheEndOfArray(array, item) {
  133. return array.push(item);
  134. }
  135.  
  136. //TODO: comment
  137. function printNumber(number) {
  138. //console.log(number);
  139. print(number);
  140. }
  141.  
  142. //TODO: comment
  143. function adjustFromOneBasedToZeroBasedNumber(number) {
  144. return sum(number, -1);
  145. }
  146.  
  147. //TODO: comment
  148. function doBusinessLogic() {
  149. var A = createArray();
  150. rangeIterate(getFirstNumberOneBased(), 368, true, function(value) { //TODO: explain magic "true" value
  151. addItemToTheEndOfArray(A, function() {
  152. printNumber(value);
  153. })
  154. });
  155.  
  156. //check
  157. arrayValuesIterate(A, function (func) {
  158. func();
  159. });
  160. }
  161.  
  162. doBusinessLogic();
  163.  
  164. //TODO: throw exceptions if call contract is violated
  165. //TODO: logging
  166. //TODO: listeners. E.g. beforeGetArrayFromIndex and afterGetArrayFromIndex, et cetera.
  167. //TODO: type annotations
  168. //TODO: move TODO section to the beginning
Success #stdin #stdout 0.55s 323136KB
stdin
Standard input is empty
stdout
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368