gleam/set

Types

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

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

Checks 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
pub fn delete(from set: Set(a), this member: a) -> Set(a)

Removes 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
pub fn filter(
  in set: Set(a),
  for property: fn(a) -> Bool,
) -> Set(a)

Creates 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]
pub fn fold(
  over set: Set(a),
  from initial: b,
  with reducer: fn(b, a) -> b,
) -> b

Combines 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
pub fn from_list(members: List(a)) -> Set(a)

Creates 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]
pub fn insert(into set: Set(a), this member: a) -> Set(a)

Inserts an member into the set.

This function runs in logarithmic time.

Examples

> new() |> insert(1) |> insert(2) |> size
2
pub fn intersection(
  of first: Set(a),
  and second: Set(a),
) -> Set(a)

Creates 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]
pub fn new() -> Set(a)

Creates a new empty set.

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

Gets the number of members in a set.

This function runs in constant time.

Examples

> new() |> insert(1) |> insert(2) |> size
2
pub fn take(from set: Set(a), keeping desired: List(a)) -> Set(a)

Creates 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]
pub fn to_list(set: Set(a)) -> List(a)

Converts 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]
pub fn union(of first: Set(a), and second: Set(a)) -> Set(a)

Creates 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]