|
| 1 | +--- |
| 2 | +title: Arrays |
| 3 | +--- |
| 4 | + |
| 5 | +An alternative to lists, arrays are fixed-length containers for ordered data. Individual elements can be swapped out for other elements, and individual elements can be retrieved quickly. |
| 6 | + |
| 7 | +## Creating and Using Arrays |
| 8 | + |
| 9 | +Like lists, arrays always contain elements of the same type, and attempting to mix element types will result in a compilation error. |
| 10 | + |
| 11 | +```grain |
| 12 | +let empty = [>] |
| 13 | +let numbers = [> 1, 2, 3] |
| 14 | +let strings = [> "foo", "bar", "baz"] |
| 15 | +``` |
| 16 | + |
| 17 | +Unlike lists, we can easily access elements at any (zero-based) index: |
| 18 | + |
| 19 | +```grain |
| 20 | +let strings = [> "foo", "bar", "baz"] |
| 21 | +
|
| 22 | +print(strings[0]) // "foo" |
| 23 | +print(strings[1]) // "bar" |
| 24 | +print(strings[2]) // "baz" |
| 25 | +``` |
| 26 | + |
| 27 | +We can also use negative indexes to access elements from the end of the array: |
| 28 | + |
| 29 | +```grain |
| 30 | +let strings = [> "foo", "bar", "baz"] |
| 31 | +
|
| 32 | +print(strings[-1]) // "baz" |
| 33 | +print(strings[-2]) // "bar" |
| 34 | +print(strings[-3]) // "foo" |
| 35 | +``` |
| 36 | + |
| 37 | +If we try to access an element beyond the length of the array, we'll get an `IndexOutOfBounds` error. |
| 38 | + |
| 39 | +## Updaing Arrays |
| 40 | + |
| 41 | +One of the major benefits arrays have is the ability to change the values it contains. We can update an array's values like so: |
| 42 | + |
| 43 | +```grain |
| 44 | +let strings = [> "foo", "bar", "baz"] |
| 45 | +
|
| 46 | +print(strings) // [> "foo", "bar", "baz"] |
| 47 | +
|
| 48 | +strings[1] = "qux" |
| 49 | +
|
| 50 | +print(strings) // [> "foo", "qux", "baz"] |
| 51 | +``` |
| 52 | + |
| 53 | +In some cases, this could allow us to write programs that are more efficient than if we used a list. |
| 54 | + |
| 55 | +However, the size of an array is fixed. To add additonal items to an array, we must append them together, which would create a brand new, third array: |
| 56 | + |
| 57 | +```grain |
| 58 | +import Array from "array" |
| 59 | +
|
| 60 | +let one = [> 1] |
| 61 | +let twoThree = [> 2, 3] |
| 62 | +let oneTwoThree = Array.append(one, twoThree) |
| 63 | +
|
| 64 | +print(oneTwoThree) // [> 1, 2, 3] |
| 65 | +``` |
| 66 | + |
| 67 | +Since `oneTwoThree` is a brand new array, updating values in `one` or `twoThree` doesn't affect the values in `oneTwoThree`. |
| 68 | + |
| 69 | +For long arrays, even when adding just one element, this could be a fairly expensive operation. For programs that need to do this kind of operation, lists may be a better choice. We discuss this in more detail in the section below. |
| 70 | + |
| 71 | +To learn more about what's available in the Array standard library, check out the [array standard library documentation](https://grain-lang.org/docs/stdlib/array). |
| 72 | + |
| 73 | +## Lists vs. Arrays |
| 74 | + |
| 75 | +Lists and arrays are similar constructs, but each has its own pros and cons. It's up to you to choose which is right for your use case! |
| 76 | + |
| 77 | +Lists are excellent because they're |
| 78 | + |
| 79 | +- immutable |
| 80 | +- efficient at adding additional elements |
| 81 | +- efficient at removing elements |
| 82 | +- easy to work with |
| 83 | +- less succeptible to bugs |
| 84 | + |
| 85 | +Lists might not be the right choice because they're |
| 86 | + |
| 87 | +- unable to be modified |
| 88 | +- inefficient at accessing random elements |
| 89 | +- inefficient at determining the number of elements |
| 90 | + |
| 91 | +Arrays are excellent because they're |
| 92 | + |
| 93 | +- able to be modified after creation |
| 94 | +- efficient at accessing random elements |
| 95 | +- efficient at determining the number of elements |
| 96 | + |
| 97 | +Arrays might not be the right choice because they're |
| 98 | + |
| 99 | +- inefficient at adding additonal elements |
| 100 | +- inefficient at removing elements |
| 101 | +- more succeptible to bugs |
| 102 | + |
| 103 | +As a rule of thumb, lists are the idiomatic choice in Grain! Lean towards using lists whenever possible and use arrays when it makes your code easier to understand. |
0 commit comments