Lua functional library: make reduce accept an operator name in place of a function, add zip
This commit is contained in:
parent
4e92cc46ad
commit
29e11e7914
1 changed files with 43 additions and 2 deletions
|
@ -47,7 +47,7 @@ function functional.choose(input, value)
|
||||||
-- Returns element of a table with the largest @value (a function)
|
-- Returns element of a table with the largest @value (a function)
|
||||||
-- Also returns the max value and the index
|
-- Also returns the max value and the index
|
||||||
|
|
||||||
local max_value, best_input, best_key = -9e99
|
local max_value, best_input, best_key = -math.huge
|
||||||
for k,v in ipairs(input) do
|
for k,v in ipairs(input) do
|
||||||
local v2 = value(v)
|
local v2 = value(v)
|
||||||
if v2 > max_value then
|
if v2 > max_value then
|
||||||
|
@ -63,7 +63,7 @@ function functional.choose_map(input, value)
|
||||||
-- Returns element of a table with the largest @value (a function)
|
-- Returns element of a table with the largest @value (a function)
|
||||||
-- Also returns the max value and the index
|
-- Also returns the max value and the index
|
||||||
|
|
||||||
local max_value, best_input, best_key = -9e99
|
local max_value, best_input, best_key = -math.huge
|
||||||
for k,v in pairs(input) do
|
for k,v in pairs(input) do
|
||||||
local v2 = value(k, v)
|
local v2 = value(k, v)
|
||||||
if v2 > max_value then
|
if v2 > max_value then
|
||||||
|
@ -86,7 +86,34 @@ function functional.map(input, formula)
|
||||||
return mapped_table
|
return mapped_table
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local known_operators = {
|
||||||
|
['+'] = function(a, b) return a + b end,
|
||||||
|
['-'] = function(a, b) return a - b end,
|
||||||
|
['*'] = function(a, b) return a * b end,
|
||||||
|
['/'] = function(a, b) return a / b end,
|
||||||
|
['%'] = function(a, b) return a % b end,
|
||||||
|
['^'] = function(a, b) return a ^ b end,
|
||||||
|
['//'] = function(a, b) return a // b end,
|
||||||
|
['&'] = function(a, b) return a & b end,
|
||||||
|
['|'] = function(a, b) return a | b end,
|
||||||
|
['~'] = function(a, b) return a ~ b end,
|
||||||
|
['<<'] = function(a, b) return a << b end,
|
||||||
|
['>>'] = function(a, b) return a >> b end,
|
||||||
|
['..'] = function(a, b) return a .. b end,
|
||||||
|
['=='] = function(a, b) return a == b end,
|
||||||
|
['~='] = function(a, b) return a ~= b end,
|
||||||
|
['<'] = function(a, b) return a < b end,
|
||||||
|
['>'] = function(a, b) return a > b end,
|
||||||
|
['<='] = function(a, b) return a <= b end,
|
||||||
|
['>='] = function(a, b) return a >= b end,
|
||||||
|
['and'] = function(a, b) return a and b end,
|
||||||
|
['or'] = function(a, b) return a or b end,
|
||||||
|
}
|
||||||
|
|
||||||
function functional.reduce(input, operator, identity)
|
function functional.reduce(input, operator, identity)
|
||||||
|
if type(operator) == 'string' then
|
||||||
|
operator = known_operators[operator]
|
||||||
|
end
|
||||||
local result = identity or 0
|
local result = identity or 0
|
||||||
for _, v in ipairs(input) do
|
for _, v in ipairs(input) do
|
||||||
result = operator(result, v)
|
result = operator(result, v)
|
||||||
|
@ -105,4 +132,18 @@ function functional.take_while(input, condition)
|
||||||
return truncated_table
|
return truncated_table
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Technically not a higher-order function, but whatever...
|
||||||
|
function functional.zip(input)
|
||||||
|
local output = {}
|
||||||
|
local _, n = functional.choose(input, function(list) return #list end)
|
||||||
|
for i = 1, n do
|
||||||
|
local elem = {}
|
||||||
|
for j, list in ipairs(input) do
|
||||||
|
elem[j] = list[i]
|
||||||
|
end
|
||||||
|
table.insert(output, elem)
|
||||||
|
end
|
||||||
|
return output
|
||||||
|
end
|
||||||
|
|
||||||
return functional
|
return functional
|
||||||
|
|
Loading…
Add table
Reference in a new issue