| 1 | """ |
| 2 | Show how to declare and use array variables, literals and sized arrays |
| 3 | """ |
| 4 | |
| 5 | class ArraysExample |
| 6 | shared |
| 7 | |
| 8 | def sum(nums as int[]) as int # nums arg is array of int |
| 9 | test |
| 10 | # Dynamic typing |
| 11 | # Inferred array type: assign from array literal; |
| 12 | # Array literal like list (enclosed in [] ) but prefixed by @ |
| 13 | nums = @[1,2,3] |
| 14 | assert nums.length == 3 |
| 15 | assert nums[2] == 3 |
| 16 | assert ArraysExample.sum(nums) == 6 |
| 17 | |
| 18 | # inferred array type: generate a 'blank' array. |
| 19 | # assign from explicitly sized array constructor |
| 20 | lottaNums = int[](10) |
| 21 | # Array contents are inited to 'default value' for the type (0 in this case) |
| 22 | # Generally a specifically sized array is made by assignment from an arrayType constructor |
| 23 | # The very general format is |
| 24 | # <var-name> = <type>(<args>) |
| 25 | # and in the case of arrays, the type is an array type (suffixed by "[]") |
| 26 | # and the arg is the desired size or length of the array, |
| 27 | # so the array allocation form is: |
| 28 | # <var-name> = <type>[](<length>) |
| 29 | # e.g nums = number[](100) |
| 30 | assert lottaNums.length == 10 |
| 31 | assert lottaNums[9] == 0 |
| 32 | assert ArraysExample.sum(lottaNums) == 0 |
| 33 | |
| 34 | # Explicit typing |
| 35 | # Init to a blank array |
| 36 | nottaLottaNums as int[] = int[](2) |
| 37 | assert nottaLottaNums.length == 2 |
| 38 | assert ArraysExample.sum(nottaLottaNums) == 0 |
| 39 | nottaLottaNums[0] = 10 |
| 40 | nottaLottaNums[1] = 11 |
| 41 | assert ArraysExample.sum(nottaLottaNums) == 21 |
| 42 | |
| 43 | # another example |
| 44 | n = 1024 |
| 45 | ch = char[](n) |
| 46 | for i in n, ch[i] = c'z' |
| 47 | |
| 48 | body |
| 49 | sum = 0 |
| 50 | for num in nums |
| 51 | sum += num |
| 52 | return sum |
| 53 | |
| 54 | def main |
| 55 | # example passing array to .Net library method |
| 56 | # .Net String.split() expects an array of chars |
| 57 | parts = 'a|b:c'.split(@[c'|', c':']) |
| 58 | assert parts.length == 3 |
| 59 | for part in parts |
| 60 | assert part in ['a','b','c'] |
| 61 | |
| 62 | |
| 63 | class BinaryFileReader |
| 64 | is abstract |
| 65 | """ |
| 66 | Abstract class that shows array handling for .Net binary file API |
| 67 | Reads a file in 256 byte chunks. |
| 68 | Subclass and define handleBuf to provide content handling |
| 69 | """ |
| 70 | shared |
| 71 | var stdin = 'stdin' |
| 72 | |
| 73 | var _fileName as String |
| 74 | |
| 75 | def init(fileName as String) |
| 76 | _fileName = fileName |
| 77 | |
| 78 | def readBinaryFile |
| 79 | if _fileName == .stdin |
| 80 | br = BinaryReader(Console.openStandardInput) |
| 81 | _readFile(br) |
| 82 | return |
| 83 | |
| 84 | if not File.exists(_fileName) |
| 85 | print "No File: [_fileName]" |
| 86 | return |
| 87 | |
| 88 | #br = BinaryReader(File.open(_fileName, FileMode.Open)) |
| 89 | br = BinaryReader(FileStream(_fileName, FileMode.Open,FileAccess.Read)) |
| 90 | _readFile(br) |
| 91 | |
| 92 | def _readFile(br as BinaryReader) |
| 93 | buffer as Byte[] = Byte[](256) # Byte array initialised to blank 256 element array. |
| 94 | #buffer as Byte[] = sharp'new Byte[256]' # alternative using C# fallthrough |
| 95 | |
| 96 | fofset=0 |
| 97 | nRead = br.read(buffer, 0, 256) |
| 98 | while nRead > 0 |
| 99 | .handleBuf(buffer, nRead, fofset) |
| 100 | fofset += nRead |
| 101 | nRead = br.read(buffer, 0, 256) |
| 102 | .fileEnd(fofset) |
| 103 | br.close |
| 104 | |
| 105 | |
| 106 | def handleBuf( buffer as Byte[], nRead as int, fOffset as int) is abstract |
| 107 | """ |
| 108 | Called on each filled or partially filled buffer |
| 109 | """ |
| 110 | |
| 111 | def fileEnd(fOffset as int) is abstract |
| 112 | """ |
| 113 | Called on eof before file closed |
| 114 | """ |
| 115 | |