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 |
#### Explicit slice with step |
82 |
shopt -s oil:all |
83 |
var mylist = [0,1,2,3,4,5,6,7,8] |
84 |
var x = mylist[slice(1, 7, 2)] |
85 |
write @x |
86 |
## STDOUT: |
87 |
1 |
88 |
3 |
89 |
5 |
90 |
## END |
91 |
|
92 |
#### Index with a Tuple |
93 |
var mydict = {[2,3]: 'foo'} |
94 |
var val = mydict[(2, 3)] |
95 |
echo $val |
96 |
# TODO: This should work! |
97 |
setvar val = mydict[2, 3] |
98 |
echo $val |
99 |
## STDOUT: |
100 |
foo |
101 |
foo |
102 |
## END |
103 |
|
104 |
#### Index with expression |
105 |
var mydict = {[5]: 3} |
106 |
var val = mydict[2+3] |
107 |
echo $val |
108 |
## STDOUT: |
109 |
3 |
110 |
## END |
111 |
|
112 |
#### Copy wtih a[:] |
113 |
var a = [1,2,3] |
114 |
var b = a[:] |
115 |
= b |
116 |
## STDOUT: |
117 |
(List) [1, 2, 3] |
118 |
## END |
119 |
|
120 |
#### Slices with Multiple Dimensions (for QTT) |
121 |
|
122 |
qtt pretty :mytable <<< ''' |
123 |
name age:Int |
124 |
alice 42 |
125 |
bob 31 |
126 |
carol 20 |
127 |
''' |
128 |
|
129 |
# Cut off the first two rows |
130 |
var t1 = mytable[2:, :] |
131 |
= t1 |
132 |
|
133 |
var t2 = mytable[:2, 3:4] |
134 |
= t2 |
135 |
|
136 |
var t3 = mytable[:2, %(name age)] |
137 |
= t3 |
138 |
|
139 |
## STDOUT: |
140 |
(Str) 'TODO: Table Slicing' |
141 |
(Str) 'TODO: Table Slicing' |
142 |
## END |
143 |
|
144 |
#### Index a list with a range, not a slice. TODO: Figure out semantics |
145 |
shopt -s oil:all |
146 |
var mylist = [1,2,3,4,5] |
147 |
var r = 1:3 |
148 |
var myslice = mylist[r] |
149 |
## status: 3 |
150 |
## STDOUT: |
151 |
TODO |
152 |
## END |
153 |
|
154 |
#### Range with list constructor |
155 |
shopt -s oil:all |
156 |
var mylist = List(0:3) |
157 |
write @mylist |
158 |
## STDOUT: |
159 |
0 |
160 |
1 |
161 |
2 |
162 |
## END |