Skip to content

Commit 2459e8e

Browse files
feat: Add reduce function to Table (#622)
feat: Add reduce function to Table
1 parent 44e4181 commit 2459e8e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/table/src/Shared/Table.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,20 @@ function Table.deepReadonly<T>(target: T): T
371371
return Table.readonly(target)
372372
end
373373

374+
--[=[
375+
Reduces a table into a single value.
376+
377+
@param target table -- Table to reduce
378+
@param reducer function -- Reducer function
379+
@param initial any -- Initial value
380+
@return any -- Result
381+
]=]
382+
function Table.reduce<T, U>(target: T, reducer: (U, T) -> U, initial: U): U
383+
local result = initial
384+
for _, item in target :: any do
385+
result = reducer(result, item)
386+
end
387+
return result
388+
end
389+
374390
return Table

0 commit comments

Comments
 (0)