# 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#L2025)

## `Set.with_capacity`

### Inline

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

### Comments

```r
#: <T: key> fn(capacity: integer) -> 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(64L), "a"))
#> 64L
```

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

## `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(list("a"))
```

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

## `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(list(2L, 1L, 2L))
#> Set.from_list(list(2L, 1L))
```

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

## `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(list("b", "a")))
#> list("b", "a")
```

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

## `Set.len`

### Inline

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

### Comments

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

The number of elements.

```r
Set.len(Set.from_list(list(1L, 1L, 2L)))
#> 2L
```

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

## `Set.is_empty`

### Inline

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

### Comments

```r
#: <T: key> fn(set: Set<T>) -> logical
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#L2074)

## `Set.contains`

### Inline

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

### Comments

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

Whether `element` is in the set.

```r
Set.contains(Set.from_list(list(1L, 2L)), 2L)
#> TRUE
```

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

## `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(1L), 2L)
#> Set.from_list(list(1L, 2L))
```

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

## `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(list(1L, 2L)), 1L)
#> Set.from_list(list(2L))
```

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

## `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(list(1L, 2L)), Set.from_list(list(2L, 3L)))
#> Set.from_list(list(1L, 2L, 3L))
```

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

## `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(list(1L, 2L)), Set.from_list(list(2L, 3L)))
#> Set.from_list(list(2L))
```

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

## `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(list(1L, 2L)), Set.from_list(list(2L, 3L)))
#> Set.from_list(list(1L))
```

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

## `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(list(1L, 2L, 3L)), 0L, function(total, x) total + x)
#> 6L
```

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

## `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(list(1L, 2L, 3L)), 0L, function(total, x) if (x > 2L) :Break(total) else :Continue(total + x))
#> 3L
```

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

## `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(list(1L, 2L, 3L)), function(x) x %% 2L)
#> Set.from_list(list(1L, 0L))
```

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

## `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(list(1L, 2L)), function(x) Set.from_list(list(x, x * 10L)))
#> Set.from_list(list(1L, 10L, 2L, 20L))
```

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

## `Set.keep_if`

### Inline

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

### Comments

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

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

```r
Set.keep_if(Set.from_list(list(1L, 2L, 3L)), function(x) x > 1L)
#> Set.from_list(list(2L, 3L))
```

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

## `Set.drop_if`

### Inline

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

### Comments

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

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

```r
Set.drop_if(Set.from_list(list(1L, 2L, 3L)), function(x) x > 1L)
#> Set.from_list(list(1L))
```

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

## `Set.reserve`

### Inline

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

### Comments

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

The set with room for `spare` more elements.

```r
Set.reserve(Set.single("a"), 1000L)
#> Set.from_list(list("a"))
```

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

## `Set.capacity`

### Inline

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

### Comments

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

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

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

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

## `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"), 9L)))
#> 1L
```

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