模块:Math:修订间差异

删除19字节 、​ 2020年4月18日 (星期六)
小
导入1个版本
wp>Xiplus
小 (已更改“模块:Math”的保护等级:高风险模板([编辑=仅管理员](无限期)[移动=仅管理员](无限期)))
 
小 (导入1个版本)
 
(未显示另一用户的1个中间版本)
第1行: 第1行:
--[[
--[[
引自维基百科(enwiki:Module:Math,oldid=613638641)


This module provides a number of basic mathematical operations.
This module provides a number of basic mathematical operations.
第49行: 第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.
第63行: 第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


第198行: 第191行:
return result
return result
end
end


--[[
--[[
第216行: 第208行:


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
第241行: 第240行:


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
第265行: 第271行:


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
第354行: 第363行:
return oldr
return oldr
end
end
local result, count = fold(findGcd, ...)
local result, count = applyFuncToArgs(findGcd, ...)
return result
return result
end
end