diff options
| author | seh <henesy.dev@gmail.com> | 2019-03-04 01:44:03 -0600 |
|---|---|---|
| committer | seh <henesy.dev@gmail.com> | 2019-03-04 01:44:03 -0600 |
| commit | 88326ef46d597b689b83ec8a44bde60ee8622537 (patch) | |
| tree | 53ab89863ad9ab28bff1fda0c9f10247a9e0ed1e /Arrays | |
| parent | 78873542e74f63d289a1a44ead0dd143a9b11fcf (diff) | |
add arrays example
Diffstat (limited to 'Arrays')
| -rw-r--r-- | Arrays/README.md | 23 | ||||
| -rw-r--r-- | Arrays/arrays.b | 7 |
2 files changed, 21 insertions, 9 deletions
diff --git a/Arrays/README.md b/Arrays/README.md index 6e559b9..6511bdb 100644 --- a/Arrays/README.md +++ b/Arrays/README.md @@ -8,31 +8,41 @@ Disclaimer: I don't like this example very much, but I'm not sure how to shore i ### arrays.b:20,30 +This shows the initialization of an array of integers using a variable, `width`, as the count of elements for the array. +Note that on `arrays.b:13,15` the array of integers, `nums`, and the integer, `width`, are declared. ### arrays.b:32,44 +Limbo supports indexing strings. The length, as per `len`, of a string is equivalent to the number of utf-8 runes within a given string. All strings in Limbo are utf-8 strings. +This section demonstrates the copying of a string into an array of bytes. ### arrays.b:46,57 +Since the size of arrays is dynamic, one can declare an array to the size of a dynamic value. The array of bytes, `dbl`, is initialized to double the size of the array `arr`. +Each index of `arr` is copied to two neighboring indices in `dbl`. ### arrays.b:59,67 - +Arrays can be initialized to a given set of values as well. In this case, the characters `[a-f]` are used as values to populate the array of strings, `chars`. The size of the array will be equal to the number of elements provided in the aforementioned set. ### arrays.b:69,84 +Arrays can be declared to be arrays of other arrays. In this case, the array `nest` is an array of four arrays of strings of size `2 << i`. +### arrays.b:86,97 -### arrays.b:86,94 - +Arrays can be conditionally initialized to values. The syntax is similar to the `case` statement syntax. In this case, the first three indices are initialized to the byte value of 4. Indices three and greater are initialized to the byte value of 0. +Note: Multiple conditional sections are not necessary to initialize to a given value. -### arrays.b:96,122 +### arrays.b:99,123 +This shows the declaration of an array of size 4 containing lists of string pairs. The lists begin as empty lists, comparable to nil. +The lists at indices 0 and 2 are prepended with pairs of strings. The head of the respective lists are then printed by extracting the pairs of strings prepended previously. ## Demo @@ -56,7 +66,7 @@ Disclaimer: I don't like this example very much, but I'm not sure how to shore i Lens: [ 2 4 8 16] Len buf: 10 - [ 3 3 3 3 3 3 3 3 3 3] + [ 4 4 4 3 3 3 3 3 3 3] Len two: 4 Lens: [ 1 0 1 0] @@ -66,4 +76,5 @@ Disclaimer: I don't like this example very much, but I'm not sure how to shore i ## Exercises - Play with the widths of different arrays, what happens? -- What can you initialize with the `* =>` operator, what can't you? +- What can you initialize to with the `* =>` operator, what can't you? +- Remove a case section from the `* =>` operator section, are all indices set to that value? diff --git a/Arrays/arrays.b b/Arrays/arrays.b index 671efff..ecbee53 100644 --- a/Arrays/arrays.b +++ b/Arrays/arrays.b @@ -84,7 +84,10 @@ init(nil: ref Draw->Context, nil: list of string) { print("]\n\n"); # Buf - buf := array[10] of {* => byte 3}; + buf := array[10] of { + 0 to 2 => byte 4, + * => byte 3 + }; print("Len buf: %d\n", len buf); @@ -96,8 +99,6 @@ init(nil: ref Draw->Context, nil: list of string) { # Two two := array[4] of list of (string, string); - #two = array[4] of list of (string, string); - two[0] = ("ducks", "quack") :: two[0]; two[2] = ("inferno", "os") :: two[2]; |
