Add iterator.(reduce|any|all)

This commit is contained in:
Lars Mueller 2022-09-30 15:14:14 +02:00
parent 19f0ac48ad
commit 94cbfe30eb

@ -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