mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-22 15:23:48 +01:00
Add iterator.(reduce|any|all)
This commit is contained in:
parent
19f0ac48ad
commit
94cbfe30eb
27
iterator.lua
27
iterator.lua
@ -62,6 +62,33 @@ function iterator.aggregate(binary_func, total, ...)
|
|||||||
return total
|
return total
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Like `iterator.aggregate`, but does not expect a `total`
|
||||||
|
function iterator.reduce(binary_func, iterator, state, control_var)
|
||||||
|
local total = iterator(state, control_var)
|
||||||
|
if total == nil then
|
||||||
|
return -- nothing if the iterator is empty
|
||||||
|
end
|
||||||
|
for value in iterator, state, total do
|
||||||
|
total = binary_func(total, value)
|
||||||
|
end
|
||||||
|
return total
|
||||||
|
end
|
||||||
|
iterator.fold = iterator.reduce
|
||||||
|
|
||||||
|
function iterator.any(...)
|
||||||
|
for val in ... do
|
||||||
|
if val then return true end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function iterator.all(...)
|
||||||
|
for val in ... do
|
||||||
|
if not val then return false end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
function iterator.min(less_than_func, ...)
|
function iterator.min(less_than_func, ...)
|
||||||
local min
|
local min
|
||||||
for value in ... do
|
for value in ... do
|
||||||
|
Loading…
Reference in New Issue
Block a user