.nib std
nib 0.3.1-pre

Pre-release documentation. This describes nib 0.3.1-pre, which is on main and has no release yet: what it documents beyond 0.3.0 may change before it ships.

module always in scope

Set

An unordered collection of distinct values, written #{1, 2, 3}. Members are kept in sorted order, and every operation answers a new Set.

Set<'t> holds each value at most once: adding a member that is already there changes nothing. Like Vec and Map, a Set is immutable — Set.add and Set.remove answer a new Set.

A literal is written #{…} with the members separated by commas, and duplicates collapse: #{1, 1, 2} is #{1, 2}. A member can be any value == can compare. The empty set needs its type written, let none: Set<Int> = #{};, because a bare #{} could also be an empty record.

Members are kept in sorted order — strings bytewise, integers numerically — whatever order they were written or added in. It is the order a Set prints in and the order Set.fold visits, so two equal sets always fold the same way.

let tags = #{"web", "nib", "docs"};
let wanted = #{"nib", "ocaml"};
(Set.intersect(tags, wanted), Set.difference(wanted, tags))
(#{"nib"}, #{"ocaml"})

Functions

funcsize
func size<'a includes Eq> :: Set<'a> -> Int

The number of members.

(Set.size(#{"a", "b", "c"}), Set.size(#{1, 1, 1}))
(3, 1)
funcadd
func add<'a includes Eq> :: Set<'a>, 'a -> Set<'a>

A new Set that also contains x. If x is already a member, the answer equals the set given.

(Set.add(#{"b"}, "a"), Set.add(#{"a"}, "a"))
(#{"a", "b"}, #{"a"})
funchas
func has<'a includes Eq> :: Set<'a>, 'a -> Bool

true if x is a member.

let primes = #{2, 3, 5, 7};
(Set.has(primes, 5), Set.has(primes, 9))
(true, false)
funcremove
func remove<'a includes Eq> :: Set<'a>, 'a -> Set<'a>

A new Set without x. Removing a value that is not a member is not an error: the answer is the same set.

(Set.remove(#{1, 2, 3}, 2), Set.remove(#{1}, 5))
(#{1, 3}, #{1})
funcunion
func union<'a includes Eq> :: Set<'a>, Set<'a> -> Set<'a>

Every value that is a member of a, of b, or of both.

Set.union(#{1, 2, 3}, #{3, 4})
#{1, 2, 3, 4}
func intersect<'a includes Eq> :: Set<'a>, Set<'a> -> Set<'a>

The values that are members of both a and b.

Set.intersect(#{1, 2, 3}, #{2, 3, 4})
#{2, 3}
func difference<'a includes Eq> :: Set<'a>, Set<'a> -> Set<'a>

The members of a that are not members of b — "a without b". The order of the arguments matters.

let a = #{1, 2, 3};
let b = #{2, 3, 4};
(Set.difference(a, b), Set.difference(b, a))
(#{1}, #{4})
funcsubset
func subset<'a includes Eq> :: Set<'a>, Set<'a> -> Bool

true if every member of a is also a member of b. A set is a subset of itself, and the empty set is a subset of every set.

let none: Set<Int> = #{};
(Set.subset(#{1, 2}, #{1, 2, 3}), Set.subset(#{1, 4}, #{1, 2, 3}), Set.subset(#{1}, #{1}), Set.subset(none, #{1}))
(true, false, true, true)
funcfold
func fold<'a includes Eq> :: Set<'a>, 'b, (('b, 'a) -> 'b) -> 'b

Combines the members in sorted order: f(acc, x) is called once per member, starting from init, and its answer is the next acc. The final acc is the result.

Set.fold(#{"pear", "apple", "fig"}, #[], { acc, x -> Vec.push(acc, x) })
#["apple", "fig", "pear"]