Stof Docs
Star on GitHubStof WebsiteDiscordContact Us
  • 🚀Welcome
  • â„šī¸Resources & Information
  • Book
    • Data Interface
    • Introduction & Design
    • Host Environments
      • Rust
      • TypeScript
        • Extend Stof
        • Import Statement
    • Data Interchange
      • JSON to Stof
      • Importing and Parsing Data
      • Exporting Data
      • REST and Restructure
    • Configuration Files
    • Schemas
      • Renaming Fields
      • Removing Fields
      • Validation & Transformation
        • Example Access
      • Nested Schemas
    • Orchestration
  • Common Concepts
    • Objects
    • Primitive Types
    • Functions
    • Fields
    • Object Types
    • Imports
    • Error Handling
    • Units
    • Casting/Conversions
  • Reference
    • CLI
      • 🏃Run
      • đŸ§ĒTest
      • 📚CLI Libraries
        • Filesystem Library
        • Time Library
        • HTTP Library
    • Stof Language
    • Libraries
      • Standard Library
      • Array/Vector Library
      • Number Library
      • String Library
      • Object Library
      • Function Library
      • Set Library
      • Map Library
      • Data Library
      • Tuple Library
      • Blob Library
      • Boolean Library
    • đŸĒ§Formats
Powered by GitBook
On this page
  • Common Value Functions
  • Set.toString(set: set): str
  • Set.or(set: set, ...): unknown
  • Set Functions
  • Set.append(set: set, other: set): void
  • Set.clear(set: set): void
  • Set.contains(set: set, val: unknown): bool
  • Set.first(set: set): unknown
  • Set.last(set: set): unknown
  • Set.insert(set: set, val: unknown): bool
  • Set.take(set: set, val: unknown): unknown
  • Set.split(set: set, val: unknown): set
  • Set.empty(set: set): bool
  • Set.any(set: set): bool
  • Set.len(set: set): int
  • Set.at(set: set, index: int): unknown
  • Set.popFirst(set: set): unknown
  • Set.popLast(set: set): unknown
  • Set.remove(set: set, val: unknown): bool
  • Set.retain(set: set, func: fn): void
  • Set.union(set: set, other: set): set
  • Set.difference(set: set, other: set): set
  • Set.intersection(set: set, other: set): set
  • Set.symmetricDifference(set: set, other: set): set
  • Set.disjoint(set: set, other: set): bool
  • Set.subset(set: set, other: set): bool
  • Set.superset(set: set, other: set): bool

Was this helpful?

  1. Reference
  2. Libraries

Set Library

Stof's standard set library ("Set").

Sets in Stof are ordered and can contain a mixture of types. Within each type, values are ordered as you would expect.

Mixed-Value Ordering:

  1. Null values

  2. Booleans

  3. Numbers

  4. Strings

  5. Objects - sorted by ID

  6. Functions - sorted by ID

  7. Arrays/Vectors

  8. Tuples

  9. Blobs

  10. Sets

  11. Maps

Common Value Functions

Set.toString(set: set): str

Returns a set converted to a string. This performs the same as "std.pln(set)" and "std.dbg(set)".

#[test("{Number(I64(1)), Number(I64(2)), Number(I64(3))}")]
fn test(): str {
    return set(1, 2, 3).toString();
}

Set.or(set: set, ...): unknown

#[test]
fn test() {
    let set = self.dne.or(set(1, 2));
    assertEq(set, set(1, 2));
}

Set Functions

Set.append(set: set, other: set): void

Move all values in "other" to this set, leaving "other" empty.

#[test]
fn append() {
    let a: set = set([1, 2]);
    let b = set([2, 3]);
    a.append(b);
    assertEq(a, set(1, 2, 3));
    assertEq(b, set(2, 3));
}

#[test]
fn append_boxed() {
    let a: Box<set> = set([1, 2]);
    let b = box(set([2, 3]));
    a.append(b);
    assertEq(a, set(1, 2, 3));
    assertEq(b, set());
}

Set.clear(set: set): void

Remove all values in this set.

#[test]
fn clear() {
    let a = set(1, 2, 3);
    assertEq(a.len(), 3);
    a.clear();
    assertEq(a.len(), 0);
}

Set.contains(set: set, val: unknown): bool

Does this set contain the value?

#[test]
fn contains() {
    let a = set(('a', 10), true, 'hello', 5);
    assert(a.contains(('a', 10)));
    assert(a.contains(true));
    assertNot(a.contains(false));
    assert(a.contains('hello'));
    assertNot(a.contains('dne'));
    assert(a.contains(5));
    assertNot(a.contains(5.1));
}

Set.first(set: set): unknown

Returns the first value in this set, or null if the set is empty.

#[test]
fn first() {
    let a = set(10, 232, 23, 1, 83, -2, 289);
    assertEq(a.first(), -2);
}

Set.last(set: set): unknown

Returns the last value in this set, or null if the set is empty.

#[test]
fn last() {
    let a = set(10, 232, 23, 1, 83, -2, 289);
    assertEq(a.last(), 289);
}

Set.insert(set: set, val: unknown): bool

Insert a value into this set, returning true if this value is newly inserted.

#[test]
fn insert() {
    let a = set();
    assert(a.insert(10));
    assertNot(a.insert(10));
    assertEq(a, set(10));
}

Set.take(set: set, val: unknown): unknown

Take a value from this set if the set contains it (removing the value from the set).

#[test]
fn take() {
    let a = set(10);
    assertEq(a.take(2), null);
    assertEq(a.take(10), 10);
    assertEq(a.len(), 0);
}

Set.split(set: set, val: unknown): set

Split this set into another set at the given value.

#[test]
fn split() {
    let a = set(0, 1, 2, 3, 4, 5, 6);
    let b = a.split(3);
    assertEq(a, set(0, 1, 2));
    assertEq(b, set(3, 4, 5, 6));

    let c = b.split(0);
    assertEq(b, set());
    assertEq(c, set(3, 4, 5, 6));

    let d = c.split(7);
    assertEq(c, set(3, 4, 5, 6));
    assertEq(d, set());
}

Set.empty(set: set): bool

Is this set empty?

#[test]
fn empty_any() {
    let a = set();
    assert(a.empty());
    assertNot(a.any());

    a.insert(10);
    assert(a.any());
    assertNot(a.empty());
}

Set.any(set: set): bool

Does this set contain any values?

#[test]
fn empty_any() {
    let a = set();
    assert(a.empty());
    assertNot(a.any());

    a.insert(10);
    assert(a.any());
    assertNot(a.empty());
}

Set.len(set: set): int

Returns the length of this set (the number of elements it contains).

#[test]
fn len() {
    let a = set();
    assertEq(a.len(), 0);
    a.insert(10);
    assertEq(a.len(), 1);
}

Set.at(set: set, index: int): unknown

Get a value in this set at the given index.

#[test]
fn at() {
    let a = set(0, 1, 2, 3, 4, 5, 6, 7);
    assertEq(a.at(1), 1);
    assertEq(a[5], 5);

    let count = 0;
    for (val in a) {
        if (first) assertEq(val, 0);
        if (last) assertEq(val, 7);
        count += 1;
    }
    assertEq(count, 8);
}

Set.popFirst(set: set): unknown

Remove the first value in the set and return it.

#[test]
fn pop_first() {
    let a = set(0, 1, 2, 3, 4);
    assertEq(a.popFirst(), 0);
    assertEq(a.popFirst(), 1);
    assertEq(a.len(), 3);
}

Set.popLast(set: set): unknown

Remove the last value in the set and return it.

#[test]
fn pop_last() {
    let a = set(0, 1, 2, 3, 4);
    assertEq(a.popLast(), 4);
    assertEq(a.popLast(), 3);
    assertEq(a.len(), 3);
}

Set.remove(set: set, val: unknown): bool

Remove a value from this set, returning true if the value existed.

#[test]
fn remove() {
    let a = set(1, 2, 3, 4);
    assert(a.remove(3));
    assertNot(a.remove(3));
    assertEq(a.len(), 3);
}

Set.retain(set: set, func: fn): void

Retain only the elements specified by the predicate.

#[test]
fn retain() {
    let a = 0..10 as set;
    a.retain((val: int): bool => val % 2 == 0);
    assertEq(a, set(0, 2, 4, 6, 8));
}

Set.union(set: set, other: set): set

Union this set with another, returning a new set.

#[test]
fn union() {
    let a = set(1, 2);
    let b = set(2, 3);

    let c = a.union(b);
    let d = a + b;
    assertEq(c, d);
    assertEq(c, set(1, 2, 3));
}

Set.difference(set: set, other: set): set

Return a new set which is the difference between this set and another.

#[test]
fn difference() {
    let a = set(1, 2);
    let b = set(2, 3);

    let c = a.difference(b);
    let d = a - b;
    assertEq(c, d);
    assertEq(c, set(1));
}

Set.intersection(set: set, other: set): set

Return a new set which is the intersection between this set and another.

#[test]
fn intersection() {
    let a = set(1, 2);
    let b = set(2, 3);

    let c = a.intersection(b);
    let d = a * b;
    assertEq(c, d);
    assertEq(c, set(2));
}

Set.symmetricDifference(set: set, other: set): set

Return a new set which is the symmetric difference between this set and another.

#[test]
fn symmetric_diff() {
    let a = set(1, 2);
    let b = set(2, 3);

    let c = a.symmetricDifference(b);
    let d = a % b;
    assertEq(c, d);
    assertEq(c, set(1, 3));
}

Set.disjoint(set: set, other: set): bool

Returns true if this set has no elements in common with another.

#[test]
fn disjoint() {
    let a = set(1, 2);
    let b = set(2, 3);
    let c = set(3, 4);
    assert(a.disjoint(c));
    assert(c.disjoint(a));
    assertNot(a.disjoint(b));
    assertNot(b.disjoint(a));
    assertNot(b.disjoint(c));
}

Set.subset(set: set, other: set): bool

Returns true if this set is a subset of another.

#[test]
fn subset() {
    let a = set(1, 2);
    let b = set(1, 2, 3);
    assert(a.subset(b));
    assertNot(b.subset(a));
}

Set.superset(set: set, other: set): bool

Returns true if this set is a superset of another.

#[test]
fn superset() {
    let a = set(1, 2);
    let b = set(1, 2, 3);
    assertNot(a.superset(b));
    assert(b.superset(a));
}
PreviousFunction LibraryNextMap Library

Last updated 4 months ago

Was this helpful?

Returns the first non-empty (null or void) argument, just like the "or" function.

Standard Library