/
loop.co
289 lines (208 loc) · 4.98 KB
/
loop.co
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
i = 5
list = while i -= 1
i * 2
eq '' + list, '8,6,4,2'
i = 5
list = (i * 3 while i -= 1)
eq '' + list, '12,9,6,3'
i = 5
func = -> i -= it
assert = -> ok false unless 0 < i < 5
results = while func 1
assert()
i
eq '' + results, '4,3,2,1'
value = false
i = 0
results = until value
value = true if i is 5
i += 1
ok i is 6
i = 5
list = []
for ever
i -= 1
break if i is 0
list.push i * 2
ok list.join(' ') is '8 6 4 2'
#759: `if` within `while` condition
2 while if 1 then 0
# https://github.com/jashkenas/coffee-script/issues/843
eq void, do -> return while 0
# Basic array comprehensions.
nums = for n of [1, 2, 3] then n * n if n & 1
results = (n * 2 for n of nums)
eq results + '', '2,18'
# Basic object comprehensions.
obj = {one: 1, two: 2, three: 3}
names = (prop + '!' for prop in obj)
odds = for prop, value in obj then prop + '!' if value & 1
eq names.join(' '), 'one! two! three!'
eq odds. join(' '), 'one! three!'
# Basic range comprehensions.
nums = (i * 3 for i from 1 to 3)
negs = (x for x from -20 to -5*2)
four = (4 for x to 3)
eq '3,6,9,-20,-19,-18,4,4,4,4', '' + nums.concat negs.slice(0, 3), four
eq '123', (i for i from 1 til 4 ).join ''
eq '036', (i for i from 0 til 9 by 3).join ''
# Never mess with binary `in`/`of`.
for i to 0
ok 0 in [0]
ok 0 of [0]
# With range comprehensions, you can loop in steps.
eq "#{ x for x from 0 to 9 by 3 }", '0,3,6,9'
eq "#{ x for x from 9 to 0 by -3 }", '9,6,3,0'
eq "#{ x for x from 3*3 to 0*0 by 0-3 }", '9,6,3,0'
# Multiline array comprehension with filter.
evens =
for num of [1, 2, 3, 4, 5, 6] then if num % 2 is 0
num *= -1
num -= 2
num * -1
eq evens + '', '4,6,8'
# Backward traversing.
odds = (num for num of [0, 1, 2, 3, 4, 5] by -2)
eq odds + '', '5,3,1'
# all/from/to/by aren't reserved off-context.
all = from = to = by = 1
# Nested comprehensions.
multiLiner =
for x from 3 to 5
for y from 3 to 5
x * y
singleLiner = (x * y for y from 3 to 5 for x from 3 to 5)
eq multiLiner.length, singleLiner.length
eq 25, multiLiner[*-1]
eq 25, singleLiner[*-1]
# Comprehensions within parentheses.
result = null
store = -> result := it
store (x * 2 for x of [3, 2, 1])
ok result.join(' ') is '6 4 2'
# Closure-wrapped comprehensions that refer to the "arguments" object.
expr = ->
result = (item * item for item of arguments)
ok expr(2, 4, 8).join(' ') is '4 16 64'
# Fast object comprehensions over all properties, including prototypal ones.
class Cat
-> @name = 'Whiskers'
breed: 'tabby'
hair: 'cream'
whiskers = new Cat
own = (value for own key, value in whiskers)
all = (value for key, value in whiskers)
ok own.join(' ') is 'Whiskers'
ok all.sort().join(' ') is 'Whiskers cream tabby'
f = -> [-> ok false, 'should cache source']
ok true for k in [f] = f()
# Comprehensions only closure-wrap their very last lines, allowing other lines
# to have `continue` or `break` in them.
func = ->
for i from 1 to 2
break if i is 2
i * j for j of [3]
eq func()[0], 3
i = 6
odds = while i--
continue unless i & 1
i
eq '5,3,1', '' + odds
r = for i from 0 to 2
switch i
case 0 then continue
case 1 then i
default break
eq r + '', '1'
eq (while 1 then break; 1).length, 0
copy = {}
continue for k, copy[k] in [4, 2]
eq copy.0 * copy.1, 8
new -> do ~>
me = this
[] = for ever
eq me, this
eq me, do ~> this
break
1
throws 'stray break on line 1' -> Coco.compile \break
throws 'stray continue on line 1' -> Coco.compile \continue
### Line folding after `for` prepositions
for x in
{2}
for y of
[3] by
-1
for z from
5 til
6
eq x*y*z, 30
### Function Plucking
# Function literals in loops are defined outside.
them = []
them.push(->) until them.1
eq ...them
### IIFE Scoping
# IIFE constructions under `for` auto-capture the loop variables.
fs = for [a, b] i of [[1 2] [3 4]]
for c of [5 6]
let
-> i + a + b + c
sums = (f() for f of fs)
eq sums.1, 9
eq sums.2, 13
fs = for x, y in {2, 3, 5} then let z = 7 then -> x * y * z
eq 63 fs.1()
os = for n of [11 13] then new -> @<<< n: -> n
eq 11 os.0.n()
### Post-`for` chains
eq "#{
a * b * c * d \
for a in {1} \
for b of [2] \
for c of [3, 4] by -1 \
for d from 5 to 6 \
for _ in {7}
}", '40,30,48,36'
### Anaphoric
while 1
break for ever
eq that, 1
break
### Destructuring `for`-`of`
r = 0
r += a * b * i for [a, b] i of [[2 3] [5 7]]
r += a + b + i for {a, b} i of [{\a \b}]
eq r, '35ab0'
### Post condition
i = 0
do
do
++i
until true
while ++i < 2
eq i, 2
(-> eq '4,2,0' ''+it) do
i * 2
while i--
### Update clause
i = 0; evens = (i while i < 9, i += 2)
eq '0,2,4,6,8' ''+evens
i = 1; odds = until i > 9, ++i
continue unless i & 1
i
eq '1,3,5,7,9' ''+odds
a = [1 2 3]
b = []
continue while a.pop(), b.push that
eq '3,2,1' ''+b
### `else` clause
for cond of [true false]
while cond
break
else
ok not cond
r = for i til 1 then i else [9]
eq 0 r.0
r = for i til 0 then i else [9]
eq 9 r.0