set

Functions for working with immutable, ordered sets (Set(a)). Sets use #[...] literal syntax and contain unique values.

Summary

FunctionSignatureDescription
contains(Set(a), a) -> Bool where a: HashCheck membership
difference(Set(a), Set(a)) -> Set(a)Elements in first but not second
each(Set(a), Fn(a) -> ()) -> ()Iterate over all elements
filter(Set(a), Fn(a) -> Bool) -> Set(a)Keep elements matching predicate
fold(Set(a), b, Fn(b, a) -> b) -> bReduce to a single value
from_list(List(a)) -> Set(a) where a: HashCreate set from list
insert(Set(a), a) -> Set(a) where a: HashAdd an element
intersection(Set(a), Set(a)) -> Set(a)Elements in both sets
is_subset(Set(a), Set(a)) -> BoolTrue if first is subset of second
length(Set(a)) -> IntNumber of elements
map(Set(a), Fn(a) -> b) -> Set(b) where b: HashTransform each element
new() -> Set(a)Create an empty set
remove(Set(a), a) -> Set(a) where a: HashRemove an element
symmetric_difference(Set(a), Set(a)) -> Set(a)Elements in exactly one of the two sets
to_list(Set(a)) -> List(a)Convert set to sorted list
union(Set(a), Set(a)) -> Set(a)Combine all elements

set.contains

set.contains(s: Set(a), elem: a) -> Bool where a: Hash

Returns true if elem is in the set.

import set

fn main() {
  let s = #[1, 2, 3]
  println(set.contains(s, 2)) -- true
  println(set.contains(s, 5)) -- false
}
open in playground

set.difference

set.difference(a: Set(a), b: Set(a)) -> Set(a)

Returns elements that are in a but not in b.

import set

fn main() {
  let diff = set.difference(#[1, 2, 3], #[2, 3, 4])
  println(set.to_list(diff)) -- [1]
}
open in playground

set.each

set.each(s: Set(a), f: Fn(a) -> ()) -> ()

Calls f for every element. Used for side effects.

import set

fn main() {
  set.each(#[1, 2, 3]) { x -> println(x) }
}
open in playground

set.filter

set.filter(s: Set(a), f: Fn(a) -> Bool) -> Set(a)

Returns a new set containing only elements for which f returns true.

import set

fn main() {
  let evens = set.filter(#[1, 2, 3, 4]) { x -> x % 2 == 0 }
  println(set.to_list(evens)) -- [2, 4]
}
open in playground

set.fold

set.fold(s: Set(a), init: b, f: Fn(b, a) -> b) -> b

Reduces the set to a single value. Iteration order is sorted.

import set

fn main() {
  let sum = set.fold(#[1, 2, 3], 0) { acc, x -> acc + x }
  println(sum) -- 6
}
open in playground

set.from_list

set.from_list(xs: List(a)) -> Set(a) where a: Hash

Creates a set from a list, removing duplicates.

import set

fn main() {
  let s = set.from_list([1, 2, 2, 3])
  println(set.length(s)) -- 3
}
open in playground

set.insert

set.insert(s: Set(a), elem: a) -> Set(a) where a: Hash

Returns a new set with elem added. No-op if already present.

import set

fn main() {
  let s = set.insert(#[1, 2], 3)
  println(set.to_list(s)) -- [1, 2, 3]
}
open in playground

set.intersection

set.intersection(a: Set(a), b: Set(a)) -> Set(a)

Returns elements that are in both a and b.

import set

fn main() {
  let common = set.intersection(#[1, 2, 3], #[2, 3, 4])
  println(set.to_list(common)) -- [2, 3]
}
open in playground

set.is_subset

set.is_subset(a: Set(a), b: Set(a)) -> Bool

Returns true if every element of a is also in b.

import set

fn main() {
  println(set.is_subset(#[1, 2], #[1, 2, 3])) -- true
  println(set.is_subset(#[1, 4], #[1, 2, 3])) -- false
}
open in playground

set.length

set.length(s: Set(a)) -> Int

Returns the number of elements in the set.

import set

fn main() {
  println(set.length(#[1, 2, 3])) -- 3
}
open in playground

set.map

set.map(s: Set(a), f: Fn(a) -> b) -> Set(b) where b: Hash

Returns a new set with f applied to each element. The result set may be smaller if f maps distinct elements to the same value.

import set

fn main() {
  let scaled = set.map(#[1, 2, 3]) { x -> x * 10 }
  println(set.to_list(scaled)) -- [10, 20, 30]
}
open in playground

set.new

set.new() -> Set(a)

Creates a new empty set.

import set

fn main() {
  let s = set.new()
  let s = set.insert(s, 42)
  println(set.length(s)) -- 1
}
open in playground

set.remove

set.remove(s: Set(a), elem: a) -> Set(a) where a: Hash

Returns a new set with elem removed. No-op if not present.

import set

fn main() {
  let s = set.remove(#[1, 2, 3], 2)
  println(set.to_list(s)) -- [1, 3]
}
open in playground

set.symmetric_difference

set.symmetric_difference(a: Set(a), b: Set(a)) -> Set(a)

Returns elements that are in exactly one of a or b — equivalent to (a - b) ∪ (b - a).

import set

fn main() {
  let diff = set.symmetric_difference(#[1, 2, 3], #[2, 3, 4])
  println(set.to_list(diff)) -- [1, 4]
}
open in playground

set.to_list

set.to_list(s: Set(a)) -> List(a)

Converts the set to a sorted list.

import set

fn main() {
  let xs = set.to_list(#[3, 1, 2])
  println(xs) -- [1, 2, 3]
}
open in playground

set.union

set.union(a: Set(a), b: Set(a)) -> Set(a)

Returns a set containing all elements from both a and b.

import set

fn main() {
  let combined = set.union(#[1, 2], #[2, 3])
  println(set.to_list(combined)) -- [1, 2, 3]
}
open in playground