Rill v0.13 Reference

Standard library · Collections

coll/set

Imported as import "coll/set" as set, its names are then set.…. Every signature below is the one the checker infers.

Types

Set(a)

A set of values: a hash table with only keys, so membership is a lookup rather than a walk. Like Map, a set is storage — add and del change it in place — and anything a map can key, a set can hold: numbers, strings, tuples, data types.

seen = set.new()
set.add(seen, "a")
set.has(seen, "a")                            # => true
set.size(set.of(Cons(1, Cons(2, Cons(2, Nil)))))   # => 2
a = set.of(Cons(1, Cons(2, Nil)))
b = set.of(Cons(2, Cons(3, Nil)))
sort(set.items(set.union(a, b)))              # => Cons(1, Cons(2, Cons(3, Nil)))
set.items(set.inter(a, b))                    # => Cons(2, Nil)
set.items(set.diff(a, b))                     # => Cons(1, Nil)
  • Set(m: Map(a, Bool))

Functions

fn new() -> Set('a) # where 'a is a value type

seen = set.new()
set.add(seen, "a")
set.has(seen, "a")   # => true
set.has(seen, "b")   # => false

fn of(items: List('a)) -> Set('a)

set.size(set.of(Cons(1, Cons(2, Cons(2, Nil)))))   # => 2

fn add(s: Set('a), x: 'a) -> Unit

s = set.new()
set.add(s, 1)
set.add(s, 1)
set.size(s)   # => 1

fn has(s: Set('a), x: 'a) -> Bool

set.has(set.of(Cons((1, 2), Nil)), (1, 2))   # => true

fn del(s: Set('a), x: 'a) -> Bool # where 'a is a value type

Removes x, saying whether it was there.

s = set.of(Cons(1, Cons(2, Nil)))
set.del(s, 1)   # => true
set.del(s, 1)   # => false
set.size(s)     # => 1

fn size(s: Set('a)) -> Int

set.size(set.of(Cons("a", Cons("b", Nil))))   # => 2

fn is_empty(s: Set('a)) -> Bool

set.is_empty(set.new())   # => true

fn items(s: Set('a)) -> List('a)

sort(set.items(set.of(Cons(3, Cons(1, Nil)))))   # => Cons(1, Cons(3, Nil))

fn clear(s: Set('a)) -> Unit # where 'a is a value type

s = set.of(Cons(1, Nil))
set.clear(s)
set.is_empty(s)   # => true

fn union(a: Set('a), b: Set('a)) -> Set('a)

sort(set.items(set.union(set.of(Cons(1, Nil)), set.of(Cons(2, Nil)))))   # => Cons(1, Cons(2, Nil))

fn inter(a: Set('a), b: Set('a)) -> Set('a)

set.items(set.inter(set.of(Cons(1, Cons(2, Nil))), set.of(Cons(2, Cons(3, Nil)))))   # => Cons(2, Nil)

fn diff(a: Set('a), b: Set('a)) -> Set('a)

What is in a and not in b.

set.items(set.diff(set.of(Cons(1, Cons(2, Nil))), set.of(Cons(2, Nil))))   # => Cons(1, Nil)

fn is_subset(a: Set('a), b: Set('a)) -> Bool

set.is_subset(set.of(Cons(1, Nil)), set.of(Cons(1, Cons(2, Nil))))   # => true

fn same(a: Set('a), b: Set('a)) -> Bool

set.same(set.of(Cons(1, Cons(2, Nil))), set.of(Cons(2, Cons(1, Nil))))   # => true

Tests

  • test_membership
  • test_any_value_is_a_member
  • test_algebra