Editing Module:Math
From MINR.ORG WIKI
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 49: | Line 49: | ||
end | end | ||
− | local function | + | local function applyFuncToArgs(func, ...) |
-- Use a function on all supplied arguments, and return the result. The function must accept two numbers as parameters, | -- Use a function on all supplied arguments, and return the result. The function must accept two numbers as parameters, | ||
-- and must return a number as an output. This number is then supplied as input to the next function call. | -- and must return a number as an output. This number is then supplied as input to the next function call. | ||
Line 63: | Line 63: | ||
end | end | ||
return ret, count | return ret, count | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
Line 198: | Line 190: | ||
return result | return result | ||
end | end | ||
− | |||
--[[ | --[[ | ||
Line 216: | Line 207: | ||
function p._max(...) | function p._max(...) | ||
− | local | + | local function maxOfTwo(a, b) |
+ | if a > b then | ||
+ | return a | ||
+ | else | ||
+ | return b | ||
+ | end | ||
+ | end | ||
+ | local max_value = applyFuncToArgs(maxOfTwo, ...) | ||
if max_value then | if max_value then | ||
return max_value | return max_value | ||
Line 241: | Line 239: | ||
function p._min(...) | function p._min(...) | ||
− | local | + | local function minOfTwo(a, b) |
+ | if a < b then | ||
+ | return a | ||
+ | else | ||
+ | return b | ||
+ | end | ||
+ | end | ||
+ | local min_value = applyFuncToArgs(minOfTwo, ...) | ||
if min_value then | if min_value then | ||
return min_value | return min_value | ||
Line 265: | Line 270: | ||
function p._average(...) | function p._average(...) | ||
− | local | + | local function getSum(a, b) |
+ | return a + b | ||
+ | end | ||
+ | local sum, count = applyFuncToArgs(getSum, ...) | ||
if not sum then | if not sum then | ||
return 0 | return 0 | ||
Line 354: | Line 362: | ||
return oldr | return oldr | ||
end | end | ||
− | local result, count = | + | local result, count = applyFuncToArgs(findGcd, ...) |
return result | return result | ||
end | end |