OScript API/Built-in Package Index

Class: Set

The Set built-in provides a set datatype with fast membership checks, and support for common set operations like union, intersection and difference.

Unlike a List, Sets contain each element only once, and have no defined order.

Class Methods

FromList( List theList )

Generates a Set from a List

Insert( Set theSet, Dynamic element )

Inserts a new element into a Set.

Intersect( Set first, Set second )

Computes the intersection of two sets

Max( Set theSet )

Returns the largest numerical element in the set.

Min( Set theSet )

Returns the smallest numerical element in the set.

Remove( Set theSet, Dynamic element )

Removes an element from the Set.

Subtract( Set first, Set second )

Subtracts the contents of one set from another

SymmetricDifference( Set first, Set second )

Computes the symmetric difference of two sets

ToList( Set theSet )

Generates a List from a Set

Union( Set first, Set second )

Computes the union of two sets

Instance Methods

Insert( Dynamic element )

Inserts an element into this Set.

Intersect( Set otherSet )

Computes the intersection of this and another set

Max( )

Returns the largest numerical element in the set.

Min( )

Returns the smallest numerical element in the set.

Remove( Dynamic element )

Removes an element from this Set.

Subtract( Set otherSet )

Subtracts the contents of another set from this one

SymmetricDifference( Set otherSet )

Computes the symmetric difference of this and another set

ToList( )

Generates a List from this Set

Union( Set otherSet )

Computes the union of this and another set

Class Methods

FromList

Set FromList( List theList )

Converts a List into a Set.

Parameters

theList

The List to convert.

Returns:

The Set generated from the List.

Example

List myList = { 'a', 'b', 'c', 'a', 1, 1, 1, 2, 3 }
Set mySet = Set.FromList( myList )
echo( mySet )

results in

set{'a','b','c',1,2,3}

Insert

Set Insert( Set theSet,
            Dynamic element )

Adds an element to the Set. Unlike Lists, Sets are mutable, so the this set is modified.

Parameters

theSet

The set to add the element to.

element

The element to add to the set.

Returns:

The Set, with the element added.

Example

Set mySet = Set.FromList({'a', 'b', 1, 3})
Set myOtherSet = Set.Insert( mySet, 'c' )
echo( mySet )
echo( myOtherSet )

results in

{'a','b','c',1,3}
{'a','b','c',1,3}

Intersect

Set Intersect( Set first,
               Set second )

Computes the intersection of two sets - the set will contain an element if and only if it was contained in both of the source sets.

Parameters

first

First set in the intersection.

second

Second set in the intersection.

Returns:

The intersection of the two sets.

Example

Set first = Set.FromList({'a', 'b', 1, 3})
Set second = Set.FromList({'a', 'c', 1, 2})
Set intersection = Set.Intersect( first, second )
echo( intersection )

results in

set{'a',1}

Max

Integer or Real Max( Set theSet )

Returns the largest numerical element in the set. See List.Max().

Parameters

theSet

Set to find the largest numerical value from.

Returns:

Largest numerical element in the set.

Min

Integer or Real Min( Set theSet )

Returns the smallest numerical element in the set. See List.Min().

Parameters

theSet

Set to find the smallest numerical value from.

Returns:

Smallest numerical element in the set.

Remove

Set Remove( Set theSet,
            Dynamic element )

Removes an element from the Set. Unlike Lists, Sets are mutable, so the this set is modified.

Parameters

theSet

The set to remove the element from.

element

The element to remove from the set.

Returns:

The Set, with the element removed.

Example

Set mySet = Set.FromList({'a', 'b', 1, 3})
Set myOtherSet = Set.Remove( mySet, 'a' )
echo( mySet )
echo( myOtherSet )

results in

{'b',1,3}
{'b',1,3}

Subtract

Set Subtract( Set first,
              Set second )

Subtracts the contents of the second set from the first. The set will contain an element if and only if it was contained in the first set and not in the second.

Parameters

first

The set being subtracted from.

second

The set being subtracted.

Returns:

The difference of the two sets.

Example

Set first = Set.FromList({'a', 'b', 1, 3})
Set second = Set.FromList({'a', 'c', 1, 2})
Set difference = Set.Subtract( first, second )
echo( difference )

results in

set{'b',3}

SymmetricDifference

Set SymmetricDifference( Set first,
                         Set second )

Computes the symmetric difference of two sets - the set will contain an element if and only if it was contained in only one of the source sets.

Set.SymmetricDifference( a, b )

is equivalent to

Set.Union( Set.Subtract( a, b ), Set.Subtract( b, a ) )

Parameters

first

First set in the symmetric difference.

second

Second set in the symmetric difference.

Returns:

The symmetric difference of the two sets.

Example

Set first = Set.FromList({'a', 'b', 1, 3})
Set second = Set.FromList({'a', 'c', 1, 2})
Set symdiff = Set.SymmetricDifference( first, second )
echo( symdiff )

results in

set{'b','c',2,3}

ToList

List ToList( Set theSet )

Converts a Set into a List.

Parameters

theSet

The Set to convert.

Returns:

The List generated from the Set.

Example

Set first = Set.FromList({'a', 'b', 1, 3})
Set second = Set.FromList({'a', 'c', 1, 2})
Set symdiff = Set.SymmetricDifference( first, second )
echo( Set.ToList( symdiff ) )

results in

{'a','b','c',1,2,3}

Union

Set Union( Set first,
           Set second )

Computes the union of two sets - the set will contain an element if and only if it was contained in at least one of the source sets.

Parameters

first

First set in the union.

second

Second set in the union.

Returns:

The union of the two sets.

Example

Set first = Set.FromList({'a', 'b', 1, 3})
Set second = Set.FromList({'a', 'c', 1, 2})
Set union = Set.Union( first, second )
echo( union )

results in

set{'a','b','c',1,2,3}

Instance Methods

Insert

Set Insert( Dynamic element )

Adds an element to this Set. Unlike Lists, Sets are mutable, so the this set is modified.

Parameters

element

The element to add to this set.

Returns:

This Set, with the element added.

Example

Set mySet = Set.FromList({'a', 'b', 1, 3})
Set myOtherSet = mySet.Insert( mySet, 'c' )
echo( mySet )
echo( myOtherSet )

results in

{'a','b','c',1,3}
{'a','b','c',1,3}

Intersect

Set Intersect( Set otherSet )

Computes the intersection of this and another set - the resulting set will contain an element if and only if it was contained in both of the source sets.

Parameters

otherSet

The other set in the intersection.

Returns:

The intersection of this and another set.

Example

Set first = Set.FromList({'a', 'b', 1, 3})
Set second = Set.FromList({'a', 'c', 1, 2})
Set intersection = first.Intersect( second )
echo( intersection )

results in

set{'a',1}

Max

Integer or Real Max()

Returns the largest numerical element in the set. See List.Max().

Returns:

Largest numerical element in the set.

Min

Integer or Real Min()

Returns the smallest numerical element in the set. See List.Min().

Returns:

Smallest numerical element in the set.

Remove

Set Remove( Dynamic element )

Removes an element from this Set. Unlike Lists, Sets are mutable, so the this set is modified.

Parameters

element

The element to remove from this set.

Returns:

This Set, with the element removed.

Example

Set mySet = Set.FromList({'a', 'b', 1, 3})
Set myOtherSet = mySet.Remove( mySet, 'a' )
echo( mySet )
echo( myOtherSet )

results in

{'b',1,3}
{'b',1,3}

Subtract

Set Subtract( Set otherSet )

Subtracts the contents of another set from this one. The resulting set will contain an element if and only if it was contained in the first set and not in the second.

Parameters

otherSet

The set being subtracted.

Returns:

The difference of this and another set.

Example

Set first = Set.FromList({'a', 'b', 1, 3})
Set second = Set.FromList({'a', 'c', 1, 2})
Set difference = first.Subtract( second )
echo( difference )

results in

set{'b',3}

SymmetricDifference

Set SymmetricDifference( Set otherSet )

Computes the symmetric difference of this and another set - the resulting set will contain an element if and only if it was contained in only one of the source sets.

a.SymmetricDifference( b )

is equivalent to

a.Subtract( b ).Union( b.Subtract( a ) )

Parameters

otherSet

The other set in the symmetric difference.

Returns:

The symmetric difference of this and the other set.

Example

Set first = Set.FromList({'a', 'b', 1, 3})
Set second = Set.FromList({'a', 'c', 1, 2})
Set symdiff = first.SymmetricDifference( second )
echo( symdiff )

results in

set{'b','c',2,3}

ToList

List ToList()

Converts this Set into a List.

Returns:

The List generated from this Set.

Example

Set first = Set.FromList({'a', 'b', 1, 3})
Set second = Set.FromList({'a', 'c', 1, 2})
Set symdiff = Set.SymmetricDifference( first, second )
echo( symdiff.ToList() )

results in

{'a','b','c',1,2,3}

Union

Set Union( Set otherSet )

Computes the union of this and another set - the resulting set will contain an element if and only if it was contained in at least one of the source sets.

Parameters

otherSet

The other set in the union.

Returns:

The union of the two sets.

Example

Set first = Set.FromList({'a', 'b', 1, 3})
Set second = Set.FromList({'a', 'c', 1, 2})
Set union = first.Union( second )
echo( union )

results in

set{'a','b','c',1,2,3}

 Copyright © 2022 OpenText Corporation. All rights reserved.