1 |
# Typed arrays |
2 |
|
3 |
#### integer array |
4 |
var x = @[1 2 3] |
5 |
write len=$len(x) |
6 |
## STDOUT: |
7 |
len=3 |
8 |
## END |
9 |
|
10 |
#### string array with command sub, varsub, etc. |
11 |
shopt -s oil:all |
12 |
|
13 |
var x = 1 |
14 |
var a = @[$x $(write hi) 'sq' "dq $x"] |
15 |
write len=$len(a) |
16 |
write @a |
17 |
## STDOUT: |
18 |
len=4 |
19 |
1 |
20 |
hi |
21 |
sq |
22 |
dq 1 |
23 |
## END |
24 |
|
25 |
#### arrays with expressions |
26 |
shopt -s oil:all |
27 |
|
28 |
# Does () make makes sense? |
29 |
|
30 |
var x = 5 |
31 |
var y = 6 |
32 |
var a = @[(x) (x+1) (y*2)] |
33 |
|
34 |
write len=$len(a) |
35 |
write @a |
36 |
|
37 |
## STDOUT: |
38 |
len=3 |
39 |
5 |
40 |
6 |
41 |
12 |
42 |
## END |
43 |
|
44 |
#### Empty arrays and using Array[T] |
45 |
shopt -s oil:all |
46 |
|
47 |
var b = Array[Bool]() |
48 |
var i = Array[Int]() |
49 |
|
50 |
#var f = Array[Float]() |
51 |
write len=$len(b) |
52 |
write len=$len(i) |
53 |
|
54 |
var b2 = Array[Bool]([true, false]) |
55 |
write @b2 |
56 |
|
57 |
#write len=$len(f) |
58 |
## STDOUT: |
59 |
len=0 |
60 |
len=0 |
61 |
True |
62 |
False |
63 |
## END |
64 |
|
65 |
|
66 |
#### Arrays from generator expressions |
67 |
shopt -s oil:all |
68 |
|
69 |
var b = Array[Bool](true for _ in 1:3) |
70 |
|
71 |
var i = Array[Int](j+1 for j in 1:3) |
72 |
#var f = Array[Float](i * 2.5 for i in 1:3) |
73 |
write @b |
74 |
write @i |
75 |
#write @f |
76 |
## STDOUT: |
77 |
True |
78 |
True |
79 |
2 |
80 |
3 |
81 |
## END |
82 |
|
83 |
#### Standalone generator expression |
84 |
var x = (i+1 for i in 1:3) |
85 |
# This is NOT a list. TODO: This test is overspecified. |
86 |
repr x | grep -o '<generator' |
87 |
write status=$? |
88 |
## STDOUT: |
89 |
<generator |
90 |
status=0 |
91 |
## END |
92 |
|
93 |
#### typeof should show the type |
94 |
var b = @[true] |
95 |
# repr should show the type of the object? |
96 |
repr b |
97 |
#typeof b |
98 |
|
99 |
var empty = @[] |
100 |
repr empty |
101 |
|
102 |
## STDOUT: |
103 |
Array[Bool] |
104 |
Array[???] # what should this be? |
105 |
## END |