LibLinq-1.0

Enumerable.prototype:ToSet(comparison_selector)

Make a new Set filled with the contents of the current Enumerable

Parameters

comparison_selector
optional: a function to generate a unique key per element

Return value

a Set


Set.FromArguments(comparison_selector, ...)

Construct and return a new Set

Parameters

comparison_selector
optional: a function to generate a unique key per element
...
arguments to populate the set with


Set.FromSequenceOrItem(sequence_or_item)

Return a Set based on either a sequence or item passed in.
If a table is passed it, it will be wrapped rather than a new Set being made and copying all the data in.

Parameters

sequence_or_item
either a set-like table, a sequence, a Set, or another value which a Set will wrap around.

Return value

a Set

Usage

local set = Set.FromSequenceOrItem(nil) -- Set containing nil, not empty
local set = Set.FromSequenceOrItem("hey") -- Set containing "hey"
local set = Set.FromSequenceOrItem({ alpha = true, bravo = true }) -- Set containing "alpha" and "bravo"
local set = Set.FromSequenceOrItem(Enumerable.From({ 1, 2, 3 })) -- Set containing 1, 2, and 3


Set.New(sequence, comparison_selector)

Construct and return a new Set

Parameters

sequence
optional: The sequence to union the set with
comparison_selector
optional: a function to generate a unique key per element

Return value

a Set

Usage

local set = Set.New()
local set = Set.New({ 1, 2, 3, 2, 4 })
local set = Set.New(nil, tostring)
local set = Set.New({ "alpha", "Alpha", "ALPHA" }, string.upper)


Set.WrapTable(t)

Wrap a lua table such that there is parity between the passed-in table and the Set.
Unlike other tables, this will not gracefully handle nils.

Parameters

t
the table to wrap around

Return value

a Set

Usage

local set = Set.WrapTable({ [1] = true, [2] = true, [3] = true })
local set = Set.WrapTable({ hey = true, there = true })


Set.prototype:Add(item)

Add an element to the Set

Parameters

item
the element to add

Return value

whether the element was inserted properly, this is typically false if the item already exists.

Usage

local success = set:Add(5)
local success = set:Add(nil)


Set.prototype:AddMany(...)

Add multiple elements to the current Set

Parameters

...
zero or more arguments to add

Usage

set:AddMany(1, 2, 3)


Set.prototype:Clear()

Clear all elements from the Set

Usage

set:Clear()


Set.prototype:Clone()

Make a shallow clone of the Set.
If the previous Set was read-only, the clone will not be.

Usage

local other = set:Clone()


Set.prototype:ConvertToReadOnly()

Make the Set read-only, preventing any further modifications.
There is no way to convert a Set back to being modifiable.

Return value

the same Set that was made read-only

Usage

set:ConvertToReadOnly()


Set.prototype:ExceptWith(sequence)

Removes all elements in the specified sequence from the current Set

Parameters

sequence
the sequence of items to remove from the current Set

Usage

set:ExceptWith({ 1, 2, 3 })


Set.prototype:GetEnumerator()

Get the enumerator for the Set

Return value

an Enumerator


Set.prototype:IntersectWith(sequence)

Modifies the current Set to contain only items which also exist in the provided sequence

Parameters

sequence
the sequence of items to include in the current Set

Usage

set:IntersectWith({ 1, 2, 3 })


Set.prototype:IsProperSubsetOf(other)

Determines whether the current Set is a proper subset of the provided sequence

Parameters

other
a sequence to compare against

Return value

a boolean


Set.prototype:IsProperSupersetOf(other)

Determines whether the current Set is a proper superset of the provided sequence

Parameters

other
a sequence to compare against

Return value

a boolean


Set.prototype:IsReadOnly()

Return whether the Set is read-only, and thus cannot have modifications made to it.

Return value

a boolean

Usage

local read_only = set:IsReadOnly()


Set.prototype:IsSubsetOf(other)

Determines whether the current Set is a subset of the provided sequence

Parameters

other
a sequence to compare against

Return value

a boolean


Set.prototype:IsSupersetOf(other)

Determines whether the current Set is a superset of the provided sequence

Parameters

other
a sequence to compare against

Return value

a boolean


Set.prototype:Overlaps(other)

Determines whether the current Set and a provided sequence share one or more common elements

Parameters

other
a sequence to compare against

Return value

a boolean


Set.prototype:PickAndRemoveRandom()

Randomly pick a random element in the Set, remove it, and return the value.
This will error if the Set is empty.

Return value

a random value in the Set

Usage

local value = set:PickAndRemoveRandom()


Set.prototype:Remove(item)

Remove an element from the Set

Parameters

item
item to try to remove from the Set

Return value

whether succesfully removed the element

Usage

local removed = set:Remove(1)


Set.prototype:RemoveWhere(predicate)

Remove all elements which match a given predicate

Parameters

predicate
a function which is passed each element and should return true to remove, false to keep

Return value

the number of elements removed

Usage

local num = set:RemoveWhere(function(x) return x % 2 == 0 end)


Set.prototype:SetContract(contract)

Set a contract that will be verified against any existing elements and any added elements or changed values.
This is handy for if you want to verify that all values are strings or something like that. This will call :VerifyContract()

Parameters

contract
a function that is passed the element and should return whether the element is valid.

Usage

set:SetContract(function(v) return type(v) == "string" end)


Set.prototype:SetEquals(other)

Determines whether the current Set contains the same distinct elements as the provided collection

Parameters

other
a sequence to compare against

Return value

a boolean


Set.prototype:SymmetricExceptWith(sequence)

Modifies the current Set to only contain elements present in either that sequence or the other sequence, but not both.

Parameters

sequence
the sequence to compare to the current

Usage

set:SymmetricExceptWith({ 1, 2, 3 })


Set.prototype:ToTable(kind)

Convert the Set to a simple lua table, with all values being true.
This will be missing values if nil is used.

Parameters

kind
"set" or nil to return a table where all values are true or "list" to return a list-like table

Return value

a lua table

Usage

Set.New({ "hey", "there" }):ToTable()["hey"] == true
Set.New({ "hey", "there" }):ToTable('list')[1] == "hey"
Set.New({ "hey", "there" }):ToTable('set')["hey"] == true


Set.prototype:UnionWith(sequence)

Modify the current set to include the elements of the provided sequence

Parameters

sequence
the sequence to union against

Usage

set:UnionWith({ 1, 2, 3 })
set:UnionWith(Enumerable.RangeTo(1, 3))


Set.prototype:VerifyContract()

Verify that the contract for this List is valid for all elements in the List.
If there is no contract, this does nothing.

Usage

list:VerifyContract()


You must login to post a comment. Don't have an account? Register to get one!

Table of contents

  1. 1 Enumerable.prototype:ToSet(comparison_selector)
    1. 1.1 Parameters
    2. 1.2 Return value
  2. 2 Set.FromArguments(comparison_selector, ...)
    1. 2.1 Parameters
  3. 3 Set.FromSequenceOrItem(sequence_or_item)
    1. 3.1 Parameters
    2. 3.2 Return value
    3. 3.3 Usage
  4. 4 Set.New(sequence, comparison_selector)
    1. 4.1 Parameters
    2. 4.2 Return value
    3. 4.3 Usage
  5. 5 Set.WrapTable(t)
    1. 5.1 Parameters
    2. 5.2 Return value
    3. 5.3 Usage
  6. 6 Set.prototype:Add(item)
    1. 6.1 Parameters
    2. 6.2 Return value
    3. 6.3 Usage
  7. 7 Set.prototype:AddMany(...)
    1. 7.1 Parameters
    2. 7.2 Usage
  8. 8 Set.prototype:Clear()
    1. 8.1 Usage
  9. 9 Set.prototype:Clone()
    1. 9.1 Usage
  10. 10 Set.prototype:ConvertToReadOnly()
    1. 10.1 Return value
    2. 10.2 Usage
  11. 11 Set.prototype:ExceptWith(sequence)
    1. 11.1 Parameters
    2. 11.2 Usage
  12. 12 Set.prototype:GetEnumerator()
    1. 12.1 Return value
  13. 13 Set.prototype:IntersectWith(sequence)
    1. 13.1 Parameters
    2. 13.2 Usage
  14. 14 Set.prototype:IsProperSubsetOf(other)
    1. 14.1 Parameters
    2. 14.2 Return value
  15. 15 Set.prototype:IsProperSupersetOf(other)
    1. 15.1 Parameters
    2. 15.2 Return value
  16. 16 Set.prototype:IsReadOnly()
    1. 16.1 Return value
    2. 16.2 Usage
  17. 17 Set.prototype:IsSubsetOf(other)
    1. 17.1 Parameters
    2. 17.2 Return value
  18. 18 Set.prototype:IsSupersetOf(other)
    1. 18.1 Parameters
    2. 18.2 Return value
  19. 19 Set.prototype:Overlaps(other)
    1. 19.1 Parameters
    2. 19.2 Return value
  20. 20 Set.prototype:PickAndRemoveRandom()
    1. 20.1 Return value
    2. 20.2 Usage
  21. 21 Set.prototype:Remove(item)
    1. 21.1 Parameters
    2. 21.2 Return value
    3. 21.3 Usage
  22. 22 Set.prototype:RemoveWhere(predicate)
    1. 22.1 Parameters
    2. 22.2 Return value
    3. 22.3 Usage
  23. 23 Set.prototype:SetContract(contract)
    1. 23.1 Parameters
    2. 23.2 Usage
  24. 24 Set.prototype:SetEquals(other)
    1. 24.1 Parameters
    2. 24.2 Return value
  25. 25 Set.prototype:SymmetricExceptWith(sequence)
    1. 25.1 Parameters
    2. 25.2 Usage
  26. 26 Set.prototype:ToTable(kind)
    1. 26.1 Parameters
    2. 26.2 Return value
    3. 26.3 Usage
  27. 27 Set.prototype:UnionWith(sequence)
    1. 27.1 Parameters
    2. 27.2 Usage
  28. 28 Set.prototype:VerifyContract()
    1. 28.1 Usage

Facts

Date created
Aug 30, 2010
Last updated
Jan 14, 2011

Author