diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-08-18 20:00:21 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-08-18 20:00:21 -0400 |
| commit | c338c3f08c6a13242e975dd344bad63a3cec9eee (patch) | |
| tree | 4e68f126de0bd900ff0556d71cb3a8ed980d3ac9 /api/sets.md | |
| parent | c972b8ba5bd61860e294322336bc9a6e0b3b6d07 (diff) | |
Update docs
Diffstat (limited to 'api/sets.md')
| -rw-r--r-- | api/sets.md | 56 |
1 files changed, 53 insertions, 3 deletions
diff --git a/api/sets.md b/api/sets.md index 7bcd4805..6dcc9ca9 100644 --- a/api/sets.md +++ b/api/sets.md @@ -10,11 +10,61 @@ b := {20, 30} = {20} ``` -Here’s the Markdown documentation for set functions: +## Syntax ---- +Sets are written using `{}` curly braces with comma-separated items: + +```tomo +nums := {10, 20, 30} +``` + +Empty sets must specify the item type explicitly: + +```tomo +empty := {:Int} +``` + +For type annotations, a set that holds items with type `T` is written as `{T}`. + +### Comprehensions + +Similar to arrays, sets can use comprehensions: + +```tomo +set := {10*i for i in 10} +set2 := {10*i for i in 10 if i mod 2 == 0} +set3 := {-10, 10*i for i in 10} +``` + +## Accessing Items + +Sets internally store their items in an array, which you can access with the +`.items` field. This is a constant-time operation that produces an immutable +view: + +```tomo +set := {10, 20, 30} +>> set.items += [10, 20, 30] +``` + +## Iteration + +You can iterate over the items in a table like this: + +```tomo +for item in set: + ... + +for i, item in set: + ... +``` + +Set iteration operates over the value of the set when the loop began, so +modifying the set during iteration is safe and will not result in the loop +iterating over any of the new values. -## Set Functions +## Set Methods ### `has` |
