aboutsummaryrefslogtreecommitdiff
path: root/docs/lists.md
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-12-11 13:50:01 -0500
committerBruce Hill <bruce@bruce-hill.com>2025-12-11 13:52:46 -0500
commit7f8f2117799cdfa6b62909a9182b5adade1d0bd2 (patch)
tree1db466db870768e952f50572453660e090e434e0 /docs/lists.md
parent630f910563b6f27dd34a4a0496a43d32539eadcb (diff)
parent02886fab651d3f64d2c8ded5597e6c075dc69b5f (diff)
Merge branch 'dev' into constructive-reals
Diffstat (limited to 'docs/lists.md')
-rw-r--r--docs/lists.md46
1 files changed, 16 insertions, 30 deletions
diff --git a/docs/lists.md b/docs/lists.md
index d12a0b5b..2700fe81 100644
--- a/docs/lists.md
+++ b/docs/lists.md
@@ -30,17 +30,15 @@ Lists can also use comprehensions, where you specify how to dynamically create
all the elements by iteration instead of manually specifying each:
```tomo
->> [i*10 for i in (3).to(8)]
-= [30, 40, 50, 60, 70, 80]
->> [i*10 for i in (3).to(8) if i != 4]
-= [30, 50, 60, 70, 80]
+assert [i*10 for i in (3).to(8)] == [30, 40, 50, 60, 70, 80]
+assert [i*10 for i in (3).to(8) if i != 4] == [30, 50, 60, 70, 80]
```
Comprehensions can be combined with regular items or other comprehensions:
```tomo
->> [-1, i*10 for i in (3).to(8), i for i in 3]
-= [-1, 30, 40, 50, 60, 70, 80, 1, 2, 3]
+nums := [-1, i*10 for i in (3).to(8), i for i in 3]
+assert nums == [-1, 30, 40, 50, 60, 70, 80, 1, 2, 3]
```
## Length
@@ -48,8 +46,7 @@ Comprehensions can be combined with regular items or other comprehensions:
List length can be accessed by the `.length` field:
```tomo
->> [10, 20, 30].length
-= 3
+assert [10, 20, 30].length == 3
```
## Indexing
@@ -61,20 +58,15 @@ last item, `-2` is the second-to-last, and so on.
```tomo
list := [10, 20, 30, 40]
->> list[1]
-= 10?
+assert list[1] == 10?
->> list[2]
-= 20?
+assert list[2] == 20?
->> list[999]
-= none
+assert list[999] == none
->> list[-1]
-= 40?
+assert list[-1] == 40?
->> list[-2]
-= 30?
+assert list[-2] == 30?
```
If a list index of `0` or any value larger than the length of the list is
@@ -103,8 +95,7 @@ has the items from one appended to the other. This should not be confused with
the addition operator `+`, which does not work with lists.
```tomo
->> [1, 2] ++ [3, 4]
-= [1, 2, 3, 4]
+assert [1, 2] ++ [3, 4] == [1, 2, 3, 4]
```
## Implementation Details
@@ -172,24 +163,20 @@ nums[4] = 40
// Constant time operation, but increments the reference count:
tmp := nums
->> tmp
-= [10, 20, 30, 40]
+assert tmp == [10, 20, 30, 40]
// Now, a mutation will trigger a copy-on-write,
// which resets the reference count to zero:
nums[4] = 999
->> nums
-= [10, 20, 30, 999]
+assert nums == [10, 20, 30, 999]
// Because of the copy-on-write, `tmp` is unchanged:
->> tmp
-= [10, 20, 30, 40]
+assert tmp == [10, 20, 30, 40]
// Since the reference count has been reset, we can do more
// mutations without triggering another copy-on-write:
nums[4] = -1
->> nums
-= [10, 20, 30, -1]
+assert nums == [10, 20, 30, -1]
```
List reference counting is _approximate_, but will only ever err on the side
@@ -211,8 +198,7 @@ nums := @[10, 20, 30]
tmp := nums
nums.insert(40)
->> tmp
-= @[10, 20, 30, 40]
+assert tmp == @[10, 20, 30, 40]
```
Having multiple pointers to the same heap-allocated list does not cause the