# Set

{/* Generated from types/std.Rtypes by `just docs-std`. Do not edit this file. */}

A set of distinct keys, typed `Set<T>`.

```
@type Set<T>
```

| Function | Summary |
| --- | --- |
| [`Set.empty`](#setempty) | A set with no elements. |
| [`Set.with_capacity`](#setwith_capacity) | An empty set with room for `capacity` elements, so inserting that many never grows it. |
| [`Set.single`](#setsingle) | A set with one element. |
| [`Set.from_list`](#setfrom_list) | A set of the list's elements, without duplicates, in first-seen order. |
| [`Set.to_list`](#setto_list) | The elements, in insertion order. |
| [`Set.len`](#setlen) | The number of elements. |
| [`Set.is_empty`](#setis_empty) | Whether the set has no elements. |
| [`Set.contains`](#setcontains) | Whether `element` is in the set. |
| [`Set.insert`](#setinsert) | Adds an element. |
| [`Set.remove`](#setremove) | Removes an element, if it is there. |
| [`Set.union`](#setunion) | The elements of either set. |
| [`Set.intersection`](#setintersection) | The elements of both sets. |
| [`Set.difference`](#setdifference) | The elements of `first` that are not in `second`. |
| [`Set.walk`](#setwalk) | Folds over the elements. |
| [`Set.walk_until`](#setwalk_until) | Folds the elements until `f` returns `:Break(state)`. |
| [`Set.map`](#setmap) | The set of `f` of every element. |
| [`Set.join_map`](#setjoin_map) | Unions the sets `f` returns for each element. |
| [`Set.keep_if`](#setkeep_if) | The elements for which `predicate` is `TRUE`. |
| [`Set.drop_if`](#setdrop_if) | The elements for which `predicate` is `FALSE`. |
| [`Set.reserve`](#setreserve) | The set with room for `spare` more elements. |
| [`Set.capacity`](#setcapacity) | How many elements the set holds before its storage must grow. |
| [`Set.release_excess_capacity`](#setrelease_excess_capacity) | The set with no room beyond its elements. |

## `Set.empty`

### Inline

```r
Set.empty <- function<T: key>() -> Set<T>
```

### Comments

```r
#: <T: key> fn() -> Set<T>
Set.empty <- function()
```

A set with no elements.

```r
Set.empty()
#> Set.empty()
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2038)

## `Set.with_capacity`

### Inline

```r
Set.with_capacity <- function<T: key>(capacity: I64) -> Set<T>
```

### Comments

```r
#: <T: key> fn(capacity: I64) -> Set<T>
Set.with_capacity <- function(capacity)
```

An empty set with room for `capacity` elements, so inserting that many never grows it. The room
is rounded up to a power of two.

```r
Set.capacity(Set.insert(Set.with_capacity(64), "a"))
#> 64i64
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2047)

## `Set.single`

### Inline

```r
Set.single <- function<T: key>(element: T) -> Set<T>
```

### Comments

```r
#: <T: key> fn(element: T) -> Set<T>
Set.single <- function(element)
```

A set with one element.

```r
Set.single("a")
#> Set.from_list(["a"])
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2055)

## `Set.from_list`

### Inline

```r
Set.from_list <- function<T: key>(list: List<T>) -> Set<T>
```

### Comments

```r
#: <T: key> fn(list: List<T>) -> Set<T>
Set.from_list <- function(list)
```

A set of the list's elements, without duplicates, in first-seen order.

```r
Set.from_list([2, 1, 2])
#> Set.from_list([2i64, 1i64])
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2063)

## `Set.to_list`

### Inline

```r
Set.to_list <- function<T: key>(set: Set<T>) -> List<T>
```

### Comments

```r
#: <T: key> fn(set: Set<T>) -> List<T>
Set.to_list <- function(set)
```

The elements, in insertion order.

```r
Set.to_list(Set.from_list(["b", "a"]))
#> ["b", "a"]
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2071)

## `Set.len`

### Inline

```r
Set.len <- function<T: key>(set: Set<T>) -> I64
```

### Comments

```r
#: <T: key> fn(set: Set<T>) -> I64
Set.len <- function(set)
```

The number of elements.

```r
Set.len(Set.from_list([1, 1, 2]))
#> 2i64
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2079)

## `Set.is_empty`

### Inline

```r
Set.is_empty <- function<T: key>(set: Set<T>) -> Bool
```

### Comments

```r
#: <T: key> fn(set: Set<T>) -> Bool
Set.is_empty <- function(set)
```

Whether the set has no elements.

```r
Set.is_empty(Set.empty())
#> TRUE
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2087)

## `Set.contains`

### Inline

```r
Set.contains <- function<T: key>(set: Set<T>, element: T) -> Bool
```

### Comments

```r
#: <T: key> fn(set: Set<T>, element: T) -> Bool
Set.contains <- function(set, element)
```

Whether `element` is in the set.

```r
Set.contains(Set.from_list([1, 2]), 2)
#> TRUE
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2095)

## `Set.insert`

### Inline

```r
Set.insert <- function<T: key>(set: Set<T>, element: T) -> Set<T>
```

### Comments

```r
#: <T: key> fn(set: Set<T>, element: T) -> Set<T>
Set.insert <- function(set, element)
```

Adds an element.

```r
Set.insert(Set.single(1), 2)
#> Set.from_list([1, 2])
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2103)

## `Set.remove`

### Inline

```r
Set.remove <- function<T: key>(set: Set<T>, element: T) -> Set<T>
```

### Comments

```r
#: <T: key> fn(set: Set<T>, element: T) -> Set<T>
Set.remove <- function(set, element)
```

Removes an element, if it is there.

```r
Set.remove(Set.from_list([1, 2]), 1)
#> Set.from_list([2i64])
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2111)

## `Set.union`

### Inline

```r
Set.union <- function<T: key>(first: Set<T>, second: Set<T>) -> Set<T>
```

### Comments

```r
#: <T: key> fn(first: Set<T>, second: Set<T>) -> Set<T>
Set.union <- function(first, second)
```

The elements of either set.

```r
Set.union(Set.from_list([1, 2]), Set.from_list([2, 3]))
#> Set.from_list([1i64, 2i64, 3i64])
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2119)

## `Set.intersection`

### Inline

```r
Set.intersection <- function<T: key>(first: Set<T>, second: Set<T>) -> Set<T>
```

### Comments

```r
#: <T: key> fn(first: Set<T>, second: Set<T>) -> Set<T>
Set.intersection <- function(first, second)
```

The elements of both sets.

```r
Set.intersection(Set.from_list([1, 2]), Set.from_list([2, 3]))
#> Set.from_list([2i64])
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2127)

## `Set.difference`

### Inline

```r
Set.difference <- function<T: key>(first: Set<T>, second: Set<T>) -> Set<T>
```

### Comments

```r
#: <T: key> fn(first: Set<T>, second: Set<T>) -> Set<T>
Set.difference <- function(first, second)
```

The elements of `first` that are not in `second`.

```r
Set.difference(Set.from_list([1, 2]), Set.from_list([2, 3]))
#> Set.from_list([1i64])
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2135)

## `Set.walk`

### Inline

```r
Set.walk <- function<T: key, S>(set: Set<T>, state: S, f: fn(S, T) -> S) -> S
```

### Comments

```r
#: <T: key, S> fn(set: Set<T>, state: S, f: fn(S, T) -> S) -> S
Set.walk <- function(set, state, f)
```

Folds over the elements.

```r
Set.walk(Set.from_list([1, 2, 3]), 0, function(total, x) total + x)
#> 6i64
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2143)

## `Set.walk_until`

### Inline

```r
Set.walk_until <- function<T: key, S>(set: Set<T>, state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> S
```

### Comments

```r
#: <T: key, S> fn(set: Set<T>, state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> S
Set.walk_until <- function(set, state, f)
```

Folds the elements until `f` returns `:Break(state)`.

```r
Set.walk_until(Set.from_list([1, 2, 3]), 0, function(total, x) if (x > 2) :Break(total) else :Continue(total + x))
#> 3i64
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2151)

## `Set.map`

### Inline

```r
Set.map <- function<A: key, B: key>(set: Set<A>, f: fn(A) -> B) -> Set<B>
```

### Comments

```r
#: <A: key, B: key> fn(set: Set<A>, f: fn(A) -> B) -> Set<B>
Set.map <- function(set, f)
```

The set of `f` of every element.

```r
Set.map(Set.from_list([1, 2, 3]), function(x) x %% 2)
#> Set.from_list([1i64, 0i64])
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2159)

## `Set.join_map`

### Inline

```r
Set.join_map <- function<A: key, B: key>(set: Set<A>, f: fn(A) -> Set<B>) -> Set<B>
```

### Comments

```r
#: <A: key, B: key> fn(set: Set<A>, f: fn(A) -> Set<B>) -> Set<B>
Set.join_map <- function(set, f)
```

Unions the sets `f` returns for each element.

```r
Set.join_map(Set.from_list([1, 2]), function(x) Set.from_list([x, x * 10]))
#> Set.from_list([1i64, 10i64, 2i64, 20i64])
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2167)

## `Set.keep_if`

### Inline

```r
Set.keep_if <- function<T: key>(set: Set<T>, predicate: fn(T) -> Bool) -> Set<T>
```

### Comments

```r
#: <T: key> fn(set: Set<T>, predicate: fn(T) -> Bool) -> Set<T>
Set.keep_if <- function(set, predicate)
```

The elements for which `predicate` is `TRUE`.

```r
Set.keep_if(Set.from_list([1, 2, 3]), function(x) x > 1)
#> Set.from_list([2i64, 3i64])
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2175)

## `Set.drop_if`

### Inline

```r
Set.drop_if <- function<T: key>(set: Set<T>, predicate: fn(T) -> Bool) -> Set<T>
```

### Comments

```r
#: <T: key> fn(set: Set<T>, predicate: fn(T) -> Bool) -> Set<T>
Set.drop_if <- function(set, predicate)
```

The elements for which `predicate` is `FALSE`.

```r
Set.drop_if(Set.from_list([1, 2, 3]), function(x) x > 1)
#> Set.from_list([1i64])
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2183)

## `Set.reserve`

### Inline

```r
Set.reserve <- function<T: key>(set: Set<T>, spare: I64) -> Set<T>
```

### Comments

```r
#: <T: key> fn(set: Set<T>, spare: I64) -> Set<T>
Set.reserve <- function(set, spare)
```

The set with room for `spare` more elements.

```r
Set.reserve(Set.single("a"), 1000)
#> Set.from_list(["a"])
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2191)

## `Set.capacity`

### Inline

```r
Set.capacity <- function<T: key>(set: Set<T>) -> I64
```

### Comments

```r
#: <T: key> fn(set: Set<T>) -> I64
Set.capacity <- function(set)
```

How many elements the set holds before its storage must grow.

```r
Set.capacity(Set.reserve(Set.single("a"), 9))
#> 10i64
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2199)

## `Set.release_excess_capacity`

### Inline

```r
Set.release_excess_capacity <- function<T: key>(set: Set<T>) -> Set<T>
```

### Comments

```r
#: <T: key> fn(set: Set<T>) -> Set<T>
Set.release_excess_capacity <- function(set)
```

The set with no room beyond its elements. An empty set keeps its smallest index, which has room
for 4.

```r
Set.capacity(Set.release_excess_capacity(Set.reserve(Set.single("a"), 9)))
#> 1i64
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L2208)
