mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 23:46:03 +01:00
feat(array): reduce method
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
---@class Array : OxClass
|
---@class Array : OxClass
|
||||||
local Array = lib.class('Array')
|
local Array = lib.class('Array')
|
||||||
|
|
||||||
|
---@private
|
||||||
function Array:constructor(...)
|
function Array:constructor(...)
|
||||||
local arr = { ... }
|
local arr = { ... }
|
||||||
|
|
||||||
@@ -9,6 +10,7 @@ function Array:constructor(...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@private
|
||||||
function Array:__newindex(index, value)
|
function Array:__newindex(index, value)
|
||||||
if type(index) ~= 'number' then error(("Cannot insert non-number index '%s' into an array."):format(index)) end
|
if type(index) ~= 'number' then error(("Cannot insert non-number index '%s' into an array."):format(index)) end
|
||||||
|
|
||||||
@@ -148,6 +150,23 @@ function Array:shift()
|
|||||||
return table.remove(self, 1)
|
return table.remove(self, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---The "reducer" function is applied to every element within an array, with the previous element's result serving as the accumulator.\
|
||||||
|
---If an initial value is provided, it's used as the accumulator for index 1; otherwise, index 1 itself serves as the initial value, and iteration begins from index 2.
|
||||||
|
---@generic T
|
||||||
|
---@param reducer fun(accumulator: T, currentValue: T, index?: number): T
|
||||||
|
---@param initialValue? T
|
||||||
|
---@return T
|
||||||
|
function Array:reduce(reducer, initialValue)
|
||||||
|
local initialIndex = initialValue and 1 or 2
|
||||||
|
local accumulator = initialValue or self[1]
|
||||||
|
|
||||||
|
for i = initialIndex, #self do
|
||||||
|
accumulator = reducer(accumulator, self[i], i)
|
||||||
|
end
|
||||||
|
|
||||||
|
return accumulator
|
||||||
|
end
|
||||||
|
|
||||||
lib.array = Array
|
lib.array = Array
|
||||||
|
|
||||||
return lib.array
|
return lib.array
|
||||||
|
|||||||
Reference in New Issue
Block a user