Skip to content

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:

local Set = require 'engine/data/set'

Reference

new

static new(values)

Create a new set

Parameters

  • values (any[]): List of initial values (optional)

Returns

  • Set

add

add(value)

Add a value to the set

Parameters

  • value (any)

remove

remove(value)

Remove a value from the set

Parameters

  • value (any)

removeIf

removeIf(predicate)

Remove all elements matching a predicate

Parameters

  • predicate (fun(value:): any): boolean

forEach

forEach(func)

Iterate over each element in the set

Parameters

  • func (fun(value:): any): boolean|nil

union

union(other)

Get the union of this set and another

Parameters

Returns

  • Set

intersection

intersection(other)

Get the intersection of this set and another

Parameters

Returns

  • Set

size

size()

Get the number of elements in the set

Returns

  • number

contains

contains(value)

Check if set contains a value

Parameters

  • value (any)

Returns

  • boolean

toList

toList()

Return all elements as a list

Returns

  • any[]

Class: Set

new

static new(values)

Create a new set

Parameters

  • values (any[]): List of initial values (optional)

Returns

  • Set

add

add(value)

Add a value to the set

Parameters

  • value (any)

remove

remove(value)

Remove a value from the set

Parameters

  • value (any)

removeIf

removeIf(predicate)

Remove all elements matching a predicate

Parameters

  • predicate (fun(value:): any): boolean

forEach

forEach(func)

Iterate over each element in the set

Parameters

  • func (fun(value:): any): boolean|nil

union

union(other)

Get the union of this set and another

Parameters

Returns

  • Set

intersection

intersection(other)

Get the intersection of this set and another

Parameters

Returns

  • Set

size

size()

Get the number of elements in the set

Returns

  • number

contains

contains(value)

Check if set contains a value

Parameters

  • value (any)

Returns

  • boolean

toList

toList()

Return all elements as a list

Returns

  • any[]