Skip to content

Commit f0db198

Browse files
committed
feat: Document arrays and loops
1 parent 690d24e commit f0db198

File tree

3 files changed

+175
-1
lines changed

3 files changed

+175
-1
lines changed

docs_config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ docs_groups:
3838
The Guide:
3939
- guide/hello_world
4040
- guide/basics
41+
- guide/mutation
4142
- guide/functions
4243
- guide/lists
43-
- guide/mutation
44+
- guide/arrays
45+
- guide/loops
4446
- guide/data_types
4547
- guide/pattern_matching
4648
- guide/imports_exports

src/guide/arrays.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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.

src/guide/loops.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: Loops
3+
---
4+
5+
Loops allow you to repeat an action over and over (and over).
6+
7+
## While Loops
8+
9+
The most basic loop is the `while` loop. The code within the loop repeats until the condition is `false`.
10+
11+
```grain
12+
let mut i = 0
13+
14+
while (i < 10) {
15+
print("looping!")
16+
i += 1
17+
}
18+
```
19+
20+
Loops can skip to the next iteration using the `continue` keyword. For example, to print only the odd numbers from zero to ten:
21+
22+
```grain
23+
let mut i = 0
24+
25+
while (i < 10) {
26+
if (i % 2 == 0) continue
27+
print(i)
28+
i += 1
29+
}
30+
```
31+
32+
Use the `break` keyword to exit from a loop entirely:
33+
34+
```grain
35+
let mut i = 0
36+
37+
while (true) {
38+
if (i < 10) {
39+
print("forever?")
40+
i += 1
41+
} else {
42+
break
43+
}
44+
}
45+
```
46+
47+
## For Loops
48+
49+
`for` loops are just like `while` loops, just with more structure. `for` loops accept an optional initializer statement which runs before the loop, an optional condition statement which runs before each iteration, and an optional increment statement which runs after each iteration.
50+
51+
`for` loops are commonly used to iterate arrays:
52+
53+
```grain
54+
import Array from "array"
55+
56+
let strings = [> "foo", "bar", "baz"]
57+
58+
for (let mut i = 0; i < Array.length(strings); i += 1) {
59+
print(strings[i])
60+
}
61+
```
62+
63+
As all of the loop parameters are optional, they can all be omitted (though the semicolons are still required). Omitting the condition results in an infinite loop:
64+
65+
```grain
66+
for (;;) {
67+
print("forever!")
68+
}
69+
```

0 commit comments

Comments
 (0)