# gleam/set

# Types

## Set

A set is a collection of unique members of the same type.

It is implemented using the `gleam/map`

module, so inserts and lookups have
logarithmic time complexity.

pub opaque type Set(member)

# Functions

## contains

pub fn contains(in set: Set(a), this member: a) -> Bool

Check whether a set contains a given member.

This function runs in logarithmic time.

## Examples

```
> new() |> insert(2) |> contains(2)
True
> new() |> insert(2) |> contains(1)
False
```

## delete

pub fn delete(from set: Set(a), this member: a) -> Set(a)

Remove an member from a set. If the set does not contain the member then the set is returned unchanged.

This function runs in logarithmic time.

## Examples

```
> new() |> insert(2) |> delete(2) |> contains(1)
False
```

## filter

pub fn filter( in set: Set(a), for property: fn(a) -> Bool, ) -> Set(a)

Create a new set from an existing set, minus any members that a given function returns False for.

This function runs in loglinear time.

## Examples

```
> import gleam/int
> from_list([1, 4, 6, 3, 675, 44, 67])
> |> filter(for: int.is_even)
> |> to_list
[4, 6, 44]
```

## fold

pub fn fold( over set: Set(a), from initial: b, with reducer: fn(a, b) -> b, ) -> b

Combine all entries into a single value by calling a given function on each one.

Sets are not ordered so the values are not returned in any specific order. Do not write code that relies on the order entries are used by this function as it may change in later versions of Gleam or Erlang.

# Examples

```
> from_list([1, 3, 9])
> |> fold(0, fn(member, accumulator) { accumulator + member })
13
```

## from_list

pub fn from_list(members: List(a)) -> Set(a)

Create a new set of the members in a given list.

This function runs in loglinear time.

## Examples

```
> import gleam/list
> [1, 1, 2, 4, 3, 2] |> from_list |> to_list |> list.sort
[1, 3, 3, 4]
```

## insert

pub fn insert(into set: Set(a), this member: a) -> Set(a)

Insert an member into the set.

This function runs in logarithmic time.

## Examples

```
> new() |> insert(1) |> insert(2) |> size
2
```

## intersection

pub fn intersection( of first: Set(a), and second: Set(a), ) -> Set(a)

Create a new set that contains members that are present in both given sets.

This function runs in loglinear time.

## Examples

```
> intersection(from_list([1, 2]), from_list([2, 3])) |> to_list
[2]
```

## size

pub fn size(set: Set(a)) -> Int

Get the number of members in a set.

This function runs in constant time.

## Examples

```
> new() |> insert(1) |> insert(2) |> size
2
```

## take

pub fn take(from set: Set(a), keeping desired: List(a)) -> Set(a)

Create a new map from a given map, only including any members which are in a given list.

This function runs in loglinear time.

## Examples

```
> from_list([1, 2, 3]) |> take([1, 3, 5]) |> to_list
[1, 3]
```

## to_list

pub fn to_list(set: Set(a)) -> List(a)

Convert the set into a list of the contained members.

The list has no specific ordering, any unintentional ordering may change in future versions of Gleam or Erlang.

This function runs in linear time.

## Examples

```
> new() |> insert(2) |> to_list
[2]
```

## union

pub fn union(of first: Set(a), and second: Set(a)) -> Set(a)

Create a new set that contains all members of both given sets.

This function runs in loglinear time.

## Examples

```
> union(from_list([1, 2]), from_list([2, 3])) |> to_list
[1, 2, 3]
```