Skip to content

SQRT Set Operations

SQRT in Python Code Block

Code blocks in this tutorial are Python literals containing actual SQRT code for ease of testing.

Sets are fundamental to SQRT. They represent collections of strings used for tags, producers, and consumers metadata.

Download Tutorial Script

Set Literals

# Set literals
set_literal_examples = r"""
// Empty set
let EmptySet = {};

// Non-empty set with string elements
let SimpleSet = {"a", "b", "c"};

// Universal set (matches everything)
let UniversalSet = {"*"};
"""

The universal set {"*"} matches everything.

Binary Set Operations

SQRT provides both symbol and keyword forms for set operations:

# Binary set operations (symbol and keyword forms)
set_binary_examples = r"""
// Union: | or union
let Union1 = arg1.tags | {"new"};
let Union2 = arg1.tags union {"new"};

// Intersection: & or intersect
let Intersect1 = arg1.tags & {"a", "b"};
let Intersect2 = arg1.tags intersect {"a", "b"};

// Difference: - or minus
let Minus1 = arg1.tags - {"remove"};
let Minus2 = arg1.tags minus {"remove"};

// Symmetric difference: ^ or xor
let Xor1 = arg1.tags ^ {"toggle"};
let Xor2 = arg1.tags xor {"toggle"};
"""
Operation Symbol Keyword Result
Union | union Elements in either set
Intersection & intersect Elements in both sets
Difference - minus Elements in left but not right
Symmetric Difference ^ xor Elements in exactly one set

Element Operations

For adding or removing single elements:

# Element operations for single elements
set_element_examples = r"""
// Add a single element
let WithElement = arg1.tags with "added";

// Remove a single element
let WithoutElement = arg1.tags without "removed";

// Chain operations
let Chained = arg1.tags with "new" without "old";
"""

Set Aggregations

Aggregate metadata across all arguments:

# Aggregations combine meta fields across all arguments
set_aggregation_examples = r"""
// Union of all argument tags
let AllTags = union of tags from args;

// Intersection of all argument tags
let CommonTags = intersect of tags from args;

// Works with producers and consumers too
let AllProducers = union of producers from args;
let CommonConsumers = intersect of consumers from args;
"""

@args Shorthand

@args.field provides convenient syntax for aggregations:

# @args sugar syntax (shorthand for aggregations)
args_meta_examples = r"""
// @args.tags is equivalent to union of tags from args
let AllTags = @args.tags;

// Explicit union suffix
let AllTagsExplicit = @args.tags.union;

// Intersect suffix
let CommonTags = @args.tags.intersect;

// Works with all meta fields
let AllProducers = @args.producers;
let CommonConsumers = @args.consumers.intersect;
"""
Syntax Equivalent
@args.tags union of tags from args
@args.tags.union union of tags from args
@args.tags.intersect intersect of tags from args

Meta Field Access

Access metadata from arguments, result, or session:

# Accessing meta fields from arguments, result, and session
meta_access_examples = r"""
// Argument meta fields
let ArgTags = arg1.tags;
let ArgProducers = arg2.producers;
let ArgConsumers = arg3.consumers;

// Result meta fields (in result/session blocks)
tool "example" {
    result {
        @result.tags = @result.tags | {"processed"};
    }
}

// Session meta fields
tool "session_example" {
    session before {
        @session.tags = @session.tags | {"started"};
    }
}
"""

Available meta fields: tags, producers, consumers