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 5: Line 5:
 
]]
 
]]
  
local yesno, getArgs -- lazily initialized
+
local yesno = require('Module:Yesno')
 +
local getArgs = require('Module:Arguments').getArgs
  
 
local p = {} -- Holds functions to be returned from #invoke, and functions to make available to other Lua modules.
 
local p = {} -- Holds functions to be returned from #invoke, and functions to make available to other Lua modules.
Line 49: Line 50:
 
end
 
end
  
local function fold(func, ...)
+
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 64:
 
end
 
end
 
return ret, count
 
return ret, count
end
 
 
--[[
 
Fold arguments by selectively choosing values (func should return when to choose the current "dominant" value).
 
]]
 
local function binary_fold(func, ...)
 
local value = fold((function(a, b) if func(a, b) then return a else return b end end), ...)
 
return value
 
 
end
 
end
  
Line 142: Line 135:
 
local input_number;
 
local input_number;
  
if not yesno then
 
yesno = require('Module:Yesno')
 
end
 
 
if yesno(trap_fraction, true) then -- Returns true for all input except nil, false, "no", "n", "0" and a few others. See [[Module:Yesno]].
 
if yesno(trap_fraction, true) then -- Returns true for all input except nil, false, "no", "n", "0" and a few others. See [[Module:Yesno]].
 
local pos = string.find(input_string, '/', 1, true);
 
local pos = string.find(input_string, '/', 1, true);
Line 198: Line 188:
 
return result
 
return result
 
end
 
end
 
  
 
--[[
 
--[[
Line 216: Line 205:
  
 
function p._max(...)
 
function p._max(...)
local max_value = binary_fold((function(a, b) return a > b end), ...)
+
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 237:
  
 
function p._min(...)
 
function p._min(...)
local min_value = binary_fold((function(a, b) return a < b end), ...)
+
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 268:
  
 
function p._average(...)
 
function p._average(...)
local sum, count = fold((function(a, b) return a + b end), ...)
+
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 360:
 
return oldr
 
return oldr
 
end
 
end
local result, count = fold(findGcd, ...)
+
local result, count = applyFuncToArgs(findGcd, ...)
 
return result
 
return result
 
end
 
end
Line 485: Line 491:
 
-- If failed, attempt to evaluate input as an expression
 
-- If failed, attempt to evaluate input as an expression
 
if number == nil then
 
if number == nil then
local success, result = pcall(mw.ext.ParserFunctions.expr, number_string)
+
local frame = mw.getCurrentFrame()
if success then
+
local attempt = frame:preprocess('{{#expr: ' .. number_string .. '}}')
number = tonumber(result)
+
attempt = tonumber(attempt)
 +
if attempt ~= nil then
 +
number = attempt
 
number_string = tostring(number)
 
number_string = tostring(number)
 
else
 
else
Line 510: Line 518:
 
]]
 
]]
  
local mt = { __index = function(t, k)
+
local function makeWrapper(funcName)
return function(frame)
+
return function (frame)
if not getArgs then
+
local args = getArgs(frame) -- Argument processing is left to Module:Arguments. Whitespace is trimmed and blank arguments are removed.
getArgs = require('Module:Arguments').getArgs
+
return wrap[funcName](args)
end
 
return wrap[k](getArgs(frame)-- Argument processing is left to Module:Arguments. Whitespace is trimmed and blank arguments are removed.
 
 
end
 
end
end }
+
end
 +
 
 +
for funcName in pairs(wrap) do
 +
p[funcName] = makeWrapper(funcName)
 +
end
  
return setmetatable(p, mt)
+
return p

Please note that all contributions to MINR.ORG WIKI may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see MINR.ORG WIKI:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)