This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
544 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
local setDifference = require("@Sift/Set/difference") | ||
local toArray = require("@Sift/Set/toArray") | ||
local toSet = require("./toSet") | ||
|
||
--[=[ | ||
@within Array | ||
Returns a new array containing only values that are in the first array and not in any of the other arrays. | ||
```lua | ||
local array1 = { 1, 2, 3 } | ||
local array2 = { 2, 3, 4 } | ||
difference(array1, array2) -- { 1 } | ||
``` | ||
]=] | ||
local function difference<T>(array: { T }, ...: { T }): { T } | ||
local arraySet = toSet(array) | ||
local otherSets = {} | ||
|
||
for _, other in { ... } do | ||
table.insert(otherSets, toSet(other)) | ||
end | ||
|
||
local differenceSet = setDifference(arraySet, unpack(otherSets)) | ||
return toArray(differenceSet) | ||
end | ||
|
||
return difference |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
local setDifferenceSymmetric = require("@Sift/Set/differenceSymmetric") | ||
local toArray = require("@Sift/Set/toArray") | ||
local toSet = require("./toSet") | ||
|
||
--[=[ | ||
@within Array | ||
Returns a new array containing the values that are only present in one of the input array. | ||
```lua | ||
local array1 = { 1, 2, 3 } | ||
local array2 = { 2, 3, 4 } | ||
differenceSymmetric(array1, array2) -- { 1, 4 } | ||
``` | ||
]=] | ||
local function differenceSymmetric<T>(array: { T }, ...: { T }): { T } | ||
local arraySet = toSet(array) | ||
local otherSets = {} | ||
|
||
for _, other in { ... } do | ||
table.insert(otherSets, toSet(other)) | ||
end | ||
|
||
local differenceSet = setDifferenceSymmetric(arraySet, unpack(otherSets)) | ||
return toArray(differenceSet) | ||
end | ||
|
||
return differenceSymmetric |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
local copy = require("./copy") | ||
|
||
--[=[ | ||
@within Set | ||
Returns a new set with the given value added to it. | ||
```lua | ||
local set = { a = true, b = true } | ||
add(set, "c") -- { a = true, b = true, c = true } | ||
``` | ||
]=] | ||
local function add<T>(set: { [T]: boolean }, value: T): { [T]: boolean } | ||
local newSet = copy(set) | ||
newSet[value] = true | ||
|
||
return newSet | ||
end | ||
|
||
return add |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--[=[ | ||
@within Set | ||
Returns a shallow copy of the set. | ||
```lua | ||
copy({ a = true, b = true }) -- { a = true, b = true } | ||
``` | ||
]=] | ||
local function copy<T>(set: { [T]: boolean }): { [T]: boolean } | ||
local out = {} | ||
|
||
for value in set do | ||
out[value] = true | ||
end | ||
|
||
return out | ||
end | ||
|
||
return copy |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--[=[ | ||
@within Set | ||
Returns the number of elements in the set that satisfy the provided predicate. | ||
```lua | ||
local set = { a = true, b = true, c = true } | ||
count(set) -- 3 | ||
count(set, function(value) | ||
return value ~= "b" | ||
end) -- 2 | ||
``` | ||
]=] | ||
local function count<T>(set: { [T]: boolean }, predicate: ((value: T) -> boolean)?): number | ||
local counter = 0 | ||
|
||
for value in set do | ||
if not predicate or predicate(value) then | ||
counter += 1 | ||
end | ||
end | ||
|
||
return counter | ||
end | ||
|
||
return count |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
local filter = require("./filter") | ||
|
||
--[=[ | ||
@within Set | ||
Returns a new set with the given value removed. | ||
```lua | ||
local set = { a = true, b = true, c = true } | ||
delete(set, "b", "c") -- { a = true } | ||
``` | ||
]=] | ||
local function delete<T>(set: { [T]: boolean }, ...: T): { [T]: boolean } | ||
local values = { ... } | ||
|
||
return filter(set, function(value: T): boolean | ||
return table.find(values, value) == nil | ||
end) | ||
end | ||
|
||
return delete |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
local copy = require("./copy") | ||
|
||
--[=[ | ||
@within Set | ||
Returns a new set containing only values that are in the first set and not in any of the other sets. | ||
```lua | ||
local set1 = { a = true, b = true, c = true } | ||
local set2 = { b = true, c = true, d = true } | ||
difference(set1, set2) -- { a = true } | ||
``` | ||
]=] | ||
local function difference<T>(set: { [T]: boolean }, ...: { [T]: boolean }): { [T]: boolean } | ||
local out = copy(set) | ||
|
||
for _, other in { ... } do | ||
for value in other do | ||
out[value] = nil | ||
end | ||
end | ||
|
||
return out | ||
end | ||
|
||
return difference |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
local copy = require("./copy") | ||
|
||
--[=[ | ||
@within Set | ||
Returns a new set containing the values that are only present in one of the input sets. | ||
```lua | ||
local set1 = { a = true, b = true, c = true } | ||
local set2 = { b = true, c = true, d = true } | ||
differenceSymmetric(set1, set2) -- { a = true, d = true } | ||
``` | ||
]=] | ||
local function differenceSymmetric<T>( | ||
set: { [T]: boolean }, | ||
...: { [T]: boolean } | ||
): { [T]: boolean } | ||
local out = copy(set) | ||
|
||
for _, other in { ... } do | ||
for value in other do | ||
out[value] = out[value] == nil | ||
end | ||
end | ||
|
||
for value, keep in out do | ||
if not keep then | ||
out[value] = nil | ||
end | ||
end | ||
|
||
return out | ||
end | ||
|
||
return differenceSymmetric |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--[=[ | ||
@within Set | ||
Returns a new set containing only the elements of the input set for which the filterer function returns `true`. | ||
```lua | ||
filter({ a = true, b = true, c = true }, function(value) | ||
return value ~= "b" | ||
end) -- { a = true, c = true } | ||
``` | ||
]=] | ||
local function filter<T>(set: { [T]: boolean }, filterer: (value: T) -> boolean): { [T]: boolean } | ||
local out = {} | ||
|
||
for value in set do | ||
if filterer(value) then | ||
out[value] = true | ||
end | ||
end | ||
|
||
return out | ||
end | ||
|
||
return filter |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--[=[ | ||
@within Set | ||
Returns a set from an array. The set will contain all the unique values from the array. | ||
```lua | ||
fromArray({ "a", "b", "c" }) | ||
-- { a = true, b = true, c = true } | ||
``` | ||
]=] | ||
local function fromArray<T>(array: { T }): { [T]: boolean } | ||
local out = {} | ||
|
||
for _, value in array do | ||
out[value] = true | ||
end | ||
|
||
return out | ||
end | ||
|
||
return fromArray |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--[=[ | ||
@within Set | ||
Returns whether the set contains the given value. | ||
```lua | ||
has({ a = true, b = true }, "a") -- true | ||
``` | ||
]=] | ||
local function has<T>(set: { [T]: boolean }, value: T): boolean | ||
return set[value] == true | ||
end | ||
|
||
return has |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,30 @@ | ||
--[=[ | ||
@class Set | ||
A set is a collection of unique values. | ||
```lua | ||
local set = { a = true, b = true, c = true } | ||
fromArray({ 1, 2, 3 }) -- { [1] = true, [2] = true, [3] = true } | ||
``` | ||
]=] | ||
return {} | ||
local set = { | ||
add = require("./add"), | ||
copy = require("./copy"), | ||
count = require("./count"), | ||
delete = require("./delete"), | ||
difference = require("./difference"), | ||
differenceSymmetric = require("./differenceSymmetric"), | ||
filter = require("./filter"), | ||
fromArray = require("./fromArray"), | ||
has = require("./has"), | ||
intersection = require("./intersection"), | ||
isSubset = require("./isSubset"), | ||
isSuperset = require("./isSuperset"), | ||
map = require("./map"), | ||
merge = require("./merge"), | ||
partition = require("./partition"), | ||
toArray = require("./toArray"), | ||
} | ||
|
||
return set |
Oops, something went wrong.