gleam/set
Types
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 a 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 difference(
from first: Set(a),
minus second: Set(a),
) -> Set(a)
Creates a new set that contains members that are present in the first set but not the second.
Examples
difference(from_list([1, 2]), from_list([2, 3, 4])) |> to_list
// -> [1]
pub fn drop(from set: Set(a), drop disallowed: List(a)) -> Set(a)
Creates a new set from a given set with all the same entries except any entry found on the given list.
Examples
drop(from_list([1, 2, 3, 4]), [1, 3])
// -> [2, 4]
pub fn filter(
in set: Set(a),
keeping predicate: 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(accumulator, member) { 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/int
import gleam/list
[1, 1, 2, 4, 3, 2] |> from_list |> to_list |> list.sort(by: int.compare)
// -> [1, 2, 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 is_disjoint(first: Set(a), from second: Set(a)) -> Bool
Determines if two sets contain no common members
Examples
is_disjoint(from_list([1, 2, 3], from_list([4, 5, 6])))
// -> True
is_disjoint(from_list([1, 2, 3]), from_list([3, 4, 5]))
// -> False
pub fn is_subset(first: Set(a), of second: Set(a)) -> Bool
Determines if a set is fully contained by another.
Examples
is_subset(from_list([1]), from_list([1, 2]))
// -> True
is_subset(from_list([1, 2, 3]), from_list([3, 4, 5]))
// -> False
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 symmetric_difference(
of first: Set(a),
and second: Set(a),
) -> Set(a)
Creates a new set that contains members that are present in either set, but not both.
symmetric_difference(from_list([1, 2, 3]), from_list([3, 4])) |> to_list
// -> [1, 2, 4]
pub fn take(from set: Set(a), keeping desired: List(a)) -> Set(a)
Creates a new set from a given set, 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]