Set
The Set class provides a powerful and efficient way to manage collections of unique items. It delivers fast lookups and ordered iteration, making it ideal for tracking and manipulating distinct elements. Quickly add and remove items, safely iterate while modifying the set, and perform essential set operations like union and intersection with other sets. With features for determining size, checking for element presence, and converting to a list, the Set class is perfect for applications needing dynamic management of unique data.
Usage
To use this class, add the following require at the top of your script:
Reference
new
Create a new set
Parameters
values(any[]): List of initial values (optional)
Returns
Set
add
Add a value to the set
Parameters
value(any)
remove
Remove a value from the set
Parameters
value(any)
removeIf
Remove all elements matching a predicate
Parameters
predicate(fun(value:): any): boolean
forEach
Iterate over each element in the set
Parameters
func(fun(value:): any): boolean|nil
union
Get the union of this set and another
Parameters
other(Set)
Returns
Set
intersection
Get the intersection of this set and another
Parameters
other(Set)
Returns
Set
size
Get the number of elements in the set
Returns
number
contains
Check if set contains a value
Parameters
value(any)
Returns
boolean
toList
Return all elements as a list
Returns
any[]
Class: Set
new
Create a new set
Parameters
values(any[]): List of initial values (optional)
Returns
Set
add
Add a value to the set
Parameters
value(any)
remove
Remove a value from the set
Parameters
value(any)
removeIf
Remove all elements matching a predicate
Parameters
predicate(fun(value:): any): boolean
forEach
Iterate over each element in the set
Parameters
func(fun(value:): any): boolean|nil
union
Get the union of this set and another
Parameters
other(Set)
Returns
Set
intersection
Get the intersection of this set and another
Parameters
other(Set)
Returns
Set
size
Get the number of elements in the set
Returns
number
contains
Check if set contains a value
Parameters
value(any)
Returns
boolean
toList
Return all elements as a list
Returns
any[]