aboutsummaryrefslogtreecommitdiff
path: root/docs/sets.md
blob: c8d67fc44ea5431715688abfa2ebb13e3a5835b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Sets

Sets represent an unordered collection of unique elements. These are
implemented using hash tables.

```tomo
a := |10, 20, 30|
b := |20, 30|
>> a.overlap(b)
= |20|
```

## Syntax

Sets are written using `|...|` vertical pipes with comma-separated items:

```tomo
nums := |10, 20, 30|
```

Empty sets must specify the set type explicitly:

```tomo
empty : |Int| = ||
```

For type annotations, a set that holds items with type `T` is written as `|T|`.

### Comprehensions

Similar to lists, 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 a list, 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]
```

## Length

Set length can be accessed by the `.length` field:

```tomo
>> |10, 20, 30|.length
= 3
```

## 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.

# API

[API documentation](../api/sets.md)