---
title: Std.Set
---

Immutable sets backed by Erlang's `sets` module (version 2).

## Functions

### new

```saga
fun new : Unit -> Set a
```

Creates an empty set.

### from_list

```saga
fun from_list : (items: List a) -> Set a where {a: Eq}
```

Creates a set from a list of elements.

### to_list

```saga
fun to_list : (set: Set a) -> List a
```

Returns the elements as a list.

### insert

```saga
fun insert : (elem: a) -> (set: Set a) -> Set a where {a: Eq}
```

Inserts an element into the set.

### remove

```saga
fun remove : (elem: a) -> (set: Set a) -> Set a where {a: Eq}
```

Removes an element from the set. Returns the set unchanged if not present.

### member

```saga
fun member : (elem: a) -> (set: Set a) -> Bool where {a: Eq}
```

Returns True if the element is in the set.

### size

```saga
fun size : (set: Set a) -> Int
```

Returns the number of elements in the set.

### is_empty

```saga
fun is_empty : Set a -> Bool
```

Returns True if the set has no elements.

### union

```saga
fun union : (a: Set a) -> (b: Set a) -> Set a where {a: Eq}
```

Returns the union of two sets.

### intersection

```saga
fun intersection : (a: Set a) -> (b: Set a) -> Set a where {a: Eq}
```

Returns the intersection of two sets.

### difference

```saga
fun difference : (a: Set a) -> (b: Set a) -> Set a where {a: Eq}
```

Returns elements in the first set but not the second.

### symmetric_difference

```saga
fun symmetric_difference : (a: Set a) -> (b: Set a) -> Set a where {a: Eq}
```

Returns elements in either set but not both.

### is_subset

```saga
fun is_subset : (sub: Set a) -> (super: Set a) -> Bool where {a: Eq}
```

Returns True if the first set is a subset of the second.

### map

```saga
fun map : (f: a -> b) -> (set: Set a) -> Set b where {b: Eq}
```

Applies a function to every element, returning a new set.

### filter

```saga
fun filter : (pred: a -> Bool) -> (set: Set a) -> Set a
```

Returns a set containing only elements where the predicate returns True.

### fold

```saga
fun fold : (f: b -> a -> b) -> (init: b) -> (set: Set a) -> b
```

Folds over all elements.

