1 |
# Test a[1] |
2 |
|
3 |
#### ranges have higher precedence than comparison (disabled) |
4 |
|
5 |
# This test exposed nondeterminism in CPython itself! Gah. Is this because of |
6 |
# the hashing? |
7 |
# Python xrange objects probably shouldn't even be comparable! |
8 |
# |
9 |
# = 1:3 < 1:4 |
10 |
# >>> xrange(1,3) < xrange(1,4) |
11 |
# False |
12 |
# >>> xrange(1,3) < xrange(1,4) |
13 |
# True |
14 |
|
15 |
= 1:3 |
16 |
|
17 |
## STDOUT: |
18 |
(xrange) xrange(1, 3) |
19 |
## END |
20 |
|
21 |
#### ranges have lower precedence than bitwise operators |
22 |
= 3:3|4 |
23 |
## STDOUT: |
24 |
(xrange) xrange(3, 7) |
25 |
## END |
26 |
|
27 |
#### subscript and range of array |
28 |
var myarray = @(1 2 3 4) |
29 |
= myarray[1] |
30 |
= myarray[1:3] |
31 |
|
32 |
echo 'implicit' |
33 |
= myarray[:2] |
34 |
= myarray[2:] |
35 |
|
36 |
# Stride not supported |
37 |
#= myarray[1:4:2] |
38 |
|
39 |
# Now try omitting smoe |
40 |
#= myarray[1:4:2] |
41 |
## STDOUT: |
42 |
(Str) '2' |
43 |
(List) ['2', '3'] |
44 |
implicit |
45 |
(List) ['1', '2'] |
46 |
(List) ['3', '4'] |
47 |
## END |
48 |
|
49 |
#### subscript and range of list |
50 |
var mylist = [1,2,3,4] |
51 |
= mylist[1] |
52 |
= mylist[1:3] |
53 |
|
54 |
echo 'implicit' |
55 |
= mylist[:2] |
56 |
= mylist[2:] |
57 |
## STDOUT: |
58 |
(Int) 2 |
59 |
(List) [2, 3] |
60 |
implicit |
61 |
(List) [1, 2] |
62 |
(List) [3, 4] |
63 |
## END |
64 |
|
65 |
#### expressions and negative indices |
66 |
var myarray = @(1 2 3 4 5) |
67 |
= myarray[-1] |
68 |
= myarray[-4:-2] |
69 |
|
70 |
echo 'implicit' |
71 |
= myarray[:-2] |
72 |
= myarray[-2:] |
73 |
## STDOUT: |
74 |
(Str) '5' |
75 |
(List) ['2', '3'] |
76 |
implicit |
77 |
(List) ['1', '2', '3'] |
78 |
(List) ['4', '5'] |
79 |
## END |
80 |
|
81 |
#### Range loop |
82 |
for (i in 1:3) { |
83 |
echo "i = $i" |
84 |
} |
85 |
var lower = -3 |
86 |
var upper = 2 |
87 |
for (i in lower:upper) { |
88 |
echo $i |
89 |
} |
90 |
## STDOUT: |
91 |
i = 1 |
92 |
i = 2 |
93 |
-3 |
94 |
-2 |
95 |
-1 |
96 |
0 |
97 |
1 |
98 |
## END |
99 |
|
100 |
#### Explicit range with step |
101 |
for (i in range(1, 7, 2)) { |
102 |
echo $i |
103 |
} |
104 |
## STDOUT: |
105 |
1 |
106 |
3 |
107 |
5 |
108 |
## END |
109 |
|
110 |
#### Explicit slice with step |
111 |
shopt -s oil:all |
112 |
var mylist = [0,1,2,3,4,5,6,7,8] |
113 |
var x = mylist[slice(1, 7, 2)] |
114 |
write @x |
115 |
## STDOUT: |
116 |
1 |
117 |
3 |
118 |
5 |
119 |
## END |
120 |
|
121 |
#### Index with a Tuple |
122 |
var mydict = @{[2,3]: 'foo'} |
123 |
var val = mydict[(2, 3)] |
124 |
echo $val |
125 |
# TODO: This should work! |
126 |
setvar val = mydict[2, 3] |
127 |
echo $val |
128 |
## STDOUT: |
129 |
foo |
130 |
foo |
131 |
## END |
132 |
|
133 |
#### Index with expression |
134 |
var mydict = @{[5]: 3} |
135 |
var val = mydict[2+3] |
136 |
echo $val |
137 |
## STDOUT: |
138 |
3 |
139 |
## END |
140 |
|
141 |
#### Copy wtih a[:] |
142 |
var a = [1,2,3] |
143 |
var b = a[:] |
144 |
= b |
145 |
## STDOUT: |
146 |
(List) [1, 2, 3] |
147 |
## END |
148 |
|
149 |
#### Slices with Multiple Dimensions (with Table/data frame) |
150 |
|
151 |
# This parses, but it isn't hashable. We need a type with operator overloading |
152 |
# to handle this, which we don't have. |
153 |
# |
154 |
# Data frames could be: |
155 |
# |
156 |
# df[3:5, :] rows 3 to 5, all cols |
157 |
# |
158 |
# df[3:5, @(name age)] rows 3 to 5, two cols |
159 |
|
160 |
#var b = d[3,1:] |
161 |
|
162 |
# TODO: We don't have col=value syntax |
163 |
var t = Table() |
164 |
|
165 |
# Cut off the first two rows |
166 |
var t1 = t[2:, :] |
167 |
= t1 |
168 |
|
169 |
var t2 = t[:2, 3:4] |
170 |
= t2 |
171 |
|
172 |
## STDOUT: |
173 |
(Str) 'TODO: Table Slicing' |
174 |
(Str) 'TODO: Table Slicing' |
175 |
## END |
176 |
|
177 |
#### Slice with Range |
178 |
shopt -s oil:all |
179 |
var mylist = [1,2,3,4,5] |
180 |
var r = 1:3 |
181 |
var myslice = mylist[r] |
182 |
write @myslice |
183 |
## STDOUT: |
184 |
a |
185 |
## END |
186 |
|
187 |
#### Range with list constructor |
188 |
shopt -s oil:all |
189 |
var mylist = List(0:3) |
190 |
write @mylist |
191 |
## STDOUT: |
192 |
0 |
193 |
1 |
194 |
2 |
195 |
## END |