1 | class ArraysExample |
---|
2 | """ |
---|
3 | Shows how to declare and use array variables and literals |
---|
4 | """ |
---|
5 | shared |
---|
6 | |
---|
7 | def sum(nums as int[]) as int |
---|
8 | test |
---|
9 | # dynamic typing |
---|
10 | nums = @[1, 2, 3] # assign array literal; like list but prefixed by @ |
---|
11 | assert nums.length == 3 |
---|
12 | assert nums[2] == 3 |
---|
13 | assert ArraysExample.sum(nums) == 6 |
---|
14 | |
---|
15 | lottaNums = int[](10) # dynamic typing explicitly sized array |
---|
16 | # contents inited to 'default value' 0 |
---|
17 | # sized array initializer form is done by assignment to an arrayType constructor |
---|
18 | # constructor arg is array size var = Type[](size) |
---|
19 | # lottaNums as int[10] |
---|
20 | # lottaNums as int[] = int[](10) # explicitly typed |
---|
21 | assert lottaNums.length == 10 |
---|
22 | assert lottaNums[9] == 0 |
---|
23 | assert ArraysExample.sum(lottaNums) == 0 |
---|
24 | body |
---|
25 | sum = 0 |
---|
26 | for num in nums |
---|
27 | sum += num |
---|
28 | return sum |
---|
29 | |
---|
30 | def main |
---|
31 | parts = 'a|b:c'.split(@[c'|', c':']) # split() expects an array of chars |
---|
32 | assert parts.length == 3 |
---|
33 | for part in parts |
---|
34 | print part |
---|
35 | |
---|
36 | |
---|
37 | class BinaryFileReader |
---|
38 | is abstract |
---|
39 | """ |
---|
40 | Abstract class that shows array handling for .Net binary file API |
---|
41 | Reads a file in 256 byte chunks. |
---|
42 | Subclass and define handleBuf to provide content handling |
---|
43 | """ |
---|
44 | shared |
---|
45 | var stdin = 'stdin' |
---|
46 | |
---|
47 | var _fileName as String |
---|
48 | |
---|
49 | def init(fileName as String) |
---|
50 | _fileName = fileName |
---|
51 | |
---|
52 | def readBinaryFile |
---|
53 | if _fileName == .stdin |
---|
54 | br = BinaryReader(Console.openStandardInput) |
---|
55 | ._readFile(br) |
---|
56 | return |
---|
57 | |
---|
58 | if not File.exists(_fileName) |
---|
59 | print "No File: [_fileName]" |
---|
60 | return |
---|
61 | |
---|
62 | #br = BinaryReader(File.open(_fileName, FileMode.Open)) |
---|
63 | br = BinaryReader(FileStream(_fileName, FileMode.Open,FileAccess.Read)) |
---|
64 | ._readFile(br) |
---|
65 | |
---|
66 | def _readFile(br as BinaryReader) |
---|
67 | buffer as Byte[] = Byte[](256) |
---|
68 | #buffer as Byte[256] |
---|
69 | #buffer as Byte[] = sharp'new Byte[256]' |
---|
70 | |
---|
71 | fofset=0 |
---|
72 | nRead = br.read(buffer, 0, 256) |
---|
73 | while nRead > 0 |
---|
74 | .handleBuf(buffer, nRead, fofset) |
---|
75 | fofset += nRead |
---|
76 | nRead = br.read(buffer, 0, 256) |
---|
77 | .fileEnd(fofset) |
---|
78 | br.close |
---|
79 | |
---|
80 | |
---|
81 | def handleBuf( buffer as Byte[], nRead as int, fOffset as int) is abstract |
---|
82 | """ |
---|
83 | Called on each filled or partially filled buffer |
---|
84 | """ |
---|
85 | |
---|
86 | def fileEnd(fOffset as int) is abstract |
---|
87 | """ |
---|
88 | Called on eof before file closed |
---|
89 | """ |
---|
90 | |
---|