LyArray - Ly Framework Lib - Gamely

LyArray is a code library that adds to lua 25 functions you generally found in other languages.
To access it from every script using Ly.Array LyFramework with properly setupped LyRequire is needed.
After installing you need to add it to LyFramework subfolder lib see LyRequire Documentation.

There is a LyFramework All Libs Blueprint with everything just set up and with examples of all the various libraries.

All functions but Create and Convert can be used with “.” and “:” operators so pay attention at the difference.

In every example let’s pretend to have initialized local Array = Ly.Array so now Array is not library table.

local Array = Ly.Array -- I will not do this any more so remember Array = this library.
local myArray = {1,2,3}
-- with "." 
Array.Push(myArray, 4)
-- or on its own created tables with ":" 
local lyMadeArray = Array.Create() -- Create always uses "."
lyMadeArray:Push(4, 5, 6)
-- myArray:Push(4, 5, 6) is instead error as myArray didn't inherit Array functions.
-- it could be converted to LyArrays with the "Convert" function
Array.Convert(myArray) -- Convert always uses "."
-- and now you can call LyArray functions on it.
myArray:Push(4, 5, 6)

All function featuring the optional tOut argument will use the provided table as output or if you didn’t provided it they will generate a new Array Table and return that.
functions with tArr argument accept both plain and LyArray generated arrays but you should only use the latest to call the function with “:” omitting the tArr argument.

Create(…)
Creates a new array. args nothing or initial elements separated by commas.

Convert(t)
Add Array methods to your provided table.

Copy(t)
Copies the array part of the table to a new Array.

local arr = Array.Create(1,2,3)
local copy = arr:Copy() or Array.Copy(arr)
local copy2 = copy:Copy() -- as now copy has inherited library functions.

SwapAndDelete(t, pos)
Quick deletion of array element at position pos.
Swap it with the last element before deletion.
It changes element order if you want to keep elements order use the slower table.remove.

local arr = Array.Create(1,2,3)
arr:SwapAndDelete(1)
--array now contains {3,2}

Push (tArr, …)
Insertion of multiple array elements at the end of the array.

Pop(tArr)
Removes the last element of the array and returns it.

Join (tArr, [Separator = “,”])
Return a string of the array elements separated by Separator or “,” if not provided.

IsArray (something)
Check if something is an array

Flatten (tArr, depth, [tOut])
Flattens nested a array up to selected depth
Creates a new array or appends to tOut if provided.

Find (tArr, boolPredicateFunction)
Return the first element that fulfill the boolPredicateFunction.

FindIndex (tArr, boolPredicateFunction)
Return the index of the first element that fulfill the boolPredicateFunction.

ForEach (tArr, func)
Run func on every element of the array.

Filter (tArr, boolPredicateFunction, [tOut])
Return a new array with all the elements that fulfill the boolPredicateFunction or appends to tOut if
provided

FilterIndices (tArr, boolPredicateFunction, [tOut])
Return a new array with indices of all the elements that fulfill the boolPredicateFunction or appends to
tOut if provided

Map (tArr, func , [tOut])
Calls function on every element of the array and returns the results as array.

Reduce (tArr, func(actualResult, actualValue) => newResult )
Uses the provided function to reduce to single value the array and returns this value

ReduceRight (tArr, func)
Uses the provided function to reduce to single value the array in reverse order and returns this value

IndexOf (tArr, valueToSearch, [fromIndex = 1])
Return the index of the first occurrence of search or -1 if not found.
Can eventually start from optional given index.

Concat (tArr, …)
Insert in the array the values of any other array passed as argument.

Every (tArr, boolPredicateFunc)
Return True if Every element of the array fulfill the boolPredicateFunc.

Includes (tArr, valueToSearch, [fromIndex = 1])
Return True if search is in the array optionally starting fromIndex
Negative fromIndex means start from the end of the array.

Reverse (tArr)
Reverse array element order

Slice (tArr, [start = 1], [end_ = start])
Extract a new array with elements in [start, end] range

Shift (tArr)
Removes and return the first element of the array shifting other positions.
Returns nil if the array is empty.

Unshift (tArr, …)
inserts elements at array beginning