Skip to content

Commit

Permalink
v0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tpc9000 committed Aug 19, 2024
1 parent 8dd74f4 commit dcd0000
Show file tree
Hide file tree
Showing 88 changed files with 551 additions and 151 deletions.
2 changes: 1 addition & 1 deletion sourcemap.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/Array/BinarySearch.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Binary search on an ordered array.
local function BinarySearch<T>(Array: {T}, Target: T, ReturnClosestIndex: boolean?): number?
Expand Down
5 changes: 3 additions & 2 deletions src/Array/Cut.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Cuts a chunk from an array given a starting and ending index - the difference in these indexes can be negative - faster if positive e.g. Cut(X, 1, 4) over Cut(X, 4, 1)
local function Cut<T>(Array: {T}, From: number, To: number): {T}
Expand All @@ -18,8 +19,8 @@ local function Cut<T>(Array: {T}, From: number, To: number): {T}
return Array
end

-- Faster, but table.move doesn't support iterating backwards over a range.
if (Diff > 0) then
-- Faster, but table.move doesn't support iterating backwards over a range
return table.move(Array, From, To, 1, {})
end

Expand Down
20 changes: 20 additions & 0 deletions src/Array/Equals.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--!native
--!optimize 2
--!nonstrict

--- Shallow checks if both arrays have equal elements.
local function Equals<T>(X: {any}, Y: {any}): boolean
if (#X ~= #Y) then
return false
end

for Index, Value in X do
if (Value ~= Y[Index]) then
return false
end
end

return true
end

return Equals
27 changes: 27 additions & 0 deletions src/Array/Equals.spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
return function()
local Equals = require(script.Parent.Equals)

describe("Array/Equals", function()
it("should return true for two empty tables", function()
expect(Equals({}, {})).to.equal(true)
end)

it("should return false for one item in X and none in Y", function()
expect(Equals({"Test"}, {})).to.equal(false)
end)

it("should return false for one item in Y and none in X", function()
expect(Equals({}, {"Test"})).to.equal(false)
end)

it("should return false for arrays of differnet sizes", function()
expect(Equals({1, 2, 3}, {1, 2})).to.equal(false)
expect(Equals({1, 2}, {1, 2, 3})).to.equal(false)
end)

it("should return true for two equal arrays", function()
expect(Equals({1, 2, 3}, {1, 2, 3})).to.equal(true)
expect(Equals({1}, {1})).to.equal(true)
end)
end)
end
3 changes: 2 additions & 1 deletion src/Array/Filter.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Filters an array for all items which satisfy some condition.
local function Filter<T>(Array: {T}, Condition: (T, number) -> boolean, Allocate: number?): {T}
Expand Down
3 changes: 2 additions & 1 deletion src/Array/FoldLeft.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Reduces an array to a single value from its left-most value to its right-most value.
local function FoldLeft<T>(Array: {T}, Processor: (T, T, number, number) -> T, Initial: T): T
Expand Down
3 changes: 2 additions & 1 deletion src/Array/FoldRight.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Reduces an array to a single value from its right-most value to its left-most value.
local function FoldRight<T>(Array: {T}, Processor: (T, T, number, number) -> T, Initial: T): T
Expand Down
5 changes: 2 additions & 3 deletions src/Array/GroupBy.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Groups the elements of an array into buckets based on the key returned by the grouper function.
--- Example: GroupBy({1, 2, 3, 4, 5}, function(Value) return Value % 2 end) --> {[0] = {2, 4}, [1] = {1, 3, 5}}
Expand All @@ -8,13 +9,11 @@ local function GroupBy<Entry>(Structure: {[number]: Entry}, Grouper: ((Entry, nu

for Key, Value in Structure do
local NewKey = Grouper(Value, Key)

if (NewKey == nil) then
continue
end

local Target = Result[NewKey]

if (Target) then
table.insert(Target, Value)
continue
Expand Down
9 changes: 4 additions & 5 deletions src/Array/Insert.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Inserts a value into a new array with an optional "insert at" index.
local function Insert<T>(Array: {T}, Value: T, At: number?): {T}
local NewSize = #Array + 1
local Result = table.create(NewSize)
At = At or NewSize

assert(At >= 1 and At <= NewSize, "Insert index out of array range")

table.move(Array, 1, At - 1, 1, Result)
table.move(Array, 1, At :: number - 1, 1, Result)
Result[At] = Value
table.move(Array, At, NewSize - 1, At + 1, Result)

table.move(Array, At :: number, NewSize - 1, At :: number + 1, Result)
return Result
end

Expand Down
3 changes: 2 additions & 1 deletion src/Array/IsArray.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Checks if the input table has an *array component*. Not mutually exclusive to IsMap.
local function IsArray(Structure: {[any]: any}): boolean
Expand Down
8 changes: 2 additions & 6 deletions src/Array/IsOrdered.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

local function _IsOrdered(Structure: {any}, AscendingOrDescending: boolean?): (boolean)
-- Check ascending.
Expand All @@ -8,11 +9,9 @@ local function _IsOrdered(Structure: {any}, AscendingOrDescending: boolean?): (b

for Index = 2, #Structure do
local Value = Structure[Index]

if (Value < LastValue) then
return false
end

LastValue = Value
end

Expand All @@ -24,11 +23,9 @@ local function _IsOrdered(Structure: {any}, AscendingOrDescending: boolean?): (b

for Index = 2, #Structure do
local Value = Structure[Index]

if (Value > LastValue) then
return false
end

LastValue = Value
end

Expand All @@ -43,7 +40,6 @@ local function IsOrdered(Structure: {any}, AscendingOrDescendingOrEither: boolea
if (AscendingOrDescendingOrEither == nil and Structure[1] ~= nil and Structure[2] ~= nil) then
return _IsOrdered(Structure, Structure[1] < Structure[2])
end

return _IsOrdered(Structure, AscendingOrDescendingOrEither)
end

Expand Down
3 changes: 2 additions & 1 deletion src/Array/IsPureArray.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

local IsArray = require(script.Parent:WaitForChild("IsArray"))
local IsMap = require(script.Parent.Parent:WaitForChild("Map"):WaitForChild("IsMap"))
Expand Down
8 changes: 4 additions & 4 deletions src/Array/Map.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Puts an array's values through a transformation function, mapping the outputs into a new array - nil values will be skipped & will not leave holes in the new array.
local function Map<T>(Array: {T}, Operator: (T, number) -> T?, Allocate: number?): {T}
local Result = table.create(Allocate or #Array)
local Index = 1

for ItemIndex = 1, #Array do
local Value = Array[ItemIndex]
local Transformed = Operator(Value, ItemIndex)
local Transformed = Operator(Array[ItemIndex], ItemIndex)

-- Skip nil values.
if (Transformed == nil) then
-- Skip nil values
continue
end

Expand Down
3 changes: 2 additions & 1 deletion src/Array/Mean.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

local function Mean<T>(Array: {T}, From: number?, To: number?): T
From = From or 1
Expand Down
3 changes: 2 additions & 1 deletion src/Array/Merge.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Merges two arrays together.
local function Merge<V1, V2>(Into: {V1}, New: {V2}): {V1 | V2}
Expand Down
3 changes: 2 additions & 1 deletion src/Array/MergeMany.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Merges multiple arrays together, in order.
local function MergeMany<T>(...: {T}): {T}
Expand Down
3 changes: 2 additions & 1 deletion src/Array/MutableBinaryInsert.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

local BinarySearch = require(script.Parent:WaitForChild("BinarySearch"))

Expand Down
3 changes: 2 additions & 1 deletion src/Array/MutableMerge.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Merges the second given array into the first.
local function MutableMerge(Into: {any}, New: {any})
Expand Down
3 changes: 2 additions & 1 deletion src/Array/MutableMergeMany.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Merges the second given array into the first.
local function MutableMergeMany(Into: {any}, ...: {any})
Expand Down
5 changes: 3 additions & 2 deletions src/Array/MutableReverse.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Flips all items in an array.
local function MutableReverse<T>(Array: {T})
local ArraySize = #Array

for Index = 1, math.floor(ArraySize / 2) do
for Index = 1, ArraySize // 2 do
local Other = ArraySize - Index + 1
Array[Index], Array[Other] = Array[Other], Array[Index]
end
Expand Down
3 changes: 2 additions & 1 deletion src/Array/MutableShuffle.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

local RandomGenerator = Random.new()

Expand Down
3 changes: 2 additions & 1 deletion src/Array/Percentile.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

local function Percentile<T>(OrderedArray: {T}, Percentile: number): T
return OrderedArray[math.ceil(#OrderedArray * Percentile)]
Expand Down
5 changes: 2 additions & 3 deletions src/Array/Product.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Produces a table of all possible combinations of a given dimension.
local function Product(Array, Dimension)
Expand All @@ -23,6 +24,4 @@ local function Product(Array, Dimension)
return Result
end

print(Product({1, 2}, 3))

return Product
3 changes: 2 additions & 1 deletion src/Array/Range.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

local function Range(Min: number, Max: number): {number}
if (Min > Max) then
Expand Down
8 changes: 4 additions & 4 deletions src/Array/Remove.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Removes a single element from an array.
local function Remove<T>(Array: {T}, Index: number?): {T}
Expand All @@ -15,9 +16,8 @@ local function Remove<T>(Array: {T}, Index: number?): {T}
assert(Index <= ArrayLength, "Index out of bounds")

local Result = table.create(ArrayLength - 1)
table.move(Array, 1, Index - 1, 1, Result)
table.move(Array, Index + 1, ArrayLength, Index, Result)

table.move(Array, 1, Index :: number - 1, 1, Result)
table.move(Array, Index :: number + 1, ArrayLength, Index :: number, Result)
return Result
end

Expand Down
3 changes: 2 additions & 1 deletion src/Array/Reverse.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

local MutableReverse = require(script.Parent:WaitForChild("MutableReverse"))

Expand Down
4 changes: 2 additions & 2 deletions src/Array/SelectFirst.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Selects the first item in an array which satisfies some condition.
local function SelectFirst<T>(Array: {T}, Condition: (T, number) -> boolean): (T?, number?)
for Index = 1, #Array do
local Value = Array[Index]

if (Condition(Value, Index)) then
return Value, Index
end
Expand Down
4 changes: 2 additions & 2 deletions src/Array/SelectLast.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

--- Selects the last item in an array which satisfies some condition.
local function SelectLast<T>(Array: {T}, Condition: (T, number) -> boolean): (T?, number?)
for Index = #Array, 1, -1 do
local Value = Array[Index]

if (Condition(Value, Index)) then
return Value, Index
end
Expand Down
3 changes: 2 additions & 1 deletion src/Array/SelectRandom.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--!optimize 2
--!native
--!optimize 2
--!nonstrict

local RandomGen = Random.new()

Expand Down
Loading

0 comments on commit dcd0000

Please sign in to comment.