Module:Exponential search and Module:Hatnote inline: Difference between pages

From Polyphasic Sleep Wiki
(Difference between pages)
m Protected "Module:Exponential search": High-risk Lua module: used in Module:Highest archive number ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite))
 
m 1 revision imported
 
Line 1: Line 1:
-- This module provides a generic exponential search algorithm.
--------------------------------------------------------------------------------
--                              Module:Hatnote-inline                        --
--                                                                            --
-- This module produces hatnote-style links and links to related articles,    --
-- but inside a <span>, instead of the <div> used by Module:Hatnote. It      --
-- implements the {{hatnote-inline}} meta-template.                          --
--------------------------------------------------------------------------------


local checkType = require('libraryUtil').checkType
local mHatnote = require('Module:Hatnote')
local floor = math.floor
local mArguments = require('Module:Arguments')
local yesno = require('Module:Yesno')
local p = {}


local function midPoint(lower, upper)
function p.hatnoteInline (frame)
return floor(lower + (upper - lower) / 2)
local args = mArguments.getArgs(frame)
local hatnote = mHatnote.hatnote(frame)
if args.inline == nil or yesno(args.inline, true) then
local subs = {
['^<div'] = '<span',
['</div>$'] = '</span>'
}
for k, v in pairs(subs) do hatnote = string.gsub(hatnote, k, v) end
end
return hatnote
end
end


local function search(testFunc, i, lower, upper)
p.hatnote = p.hatnoteInline --alias
if testFunc(i) then
if i + 1 == upper then
return i
end
lower = i
if upper then
i = midPoint(lower, upper)
else
i = i * 2
end
return search(testFunc, i, lower, upper)
else
upper = i
i = midPoint(lower, upper)
return search(testFunc, i, lower, upper)
end
end


return function (testFunc, init)
return p
checkType('Exponential search', 1, testFunc, 'function')
checkType('Exponential search', 2, init, 'number', true)
if init and (init < 1 or init ~= floor(init) or init == math.huge) then
error(string.format(
"invalid init value '%s' detected in argument #2 to " ..
"'Exponential search' (init value must be a positive integer)",
tostring(init)
), 2)
end
init = init or 2
if not testFunc(1) then
return nil
end
return search(testFunc, init, 1, nil)
end

Latest revision as of 06:19, 20 November 2020

Documentation for this module may be created at Module:Hatnote inline/doc

--------------------------------------------------------------------------------
--                              Module:Hatnote-inline                         --
--                                                                            --
-- This module produces hatnote-style links and links to related articles,    --
-- but inside a <span>, instead of the <div> used by Module:Hatnote.  It      --
-- implements the {{hatnote-inline}} meta-template.                           --
--------------------------------------------------------------------------------

local mHatnote = require('Module:Hatnote')
local mArguments = require('Module:Arguments')
local yesno = require('Module:Yesno')
local p = {}

function p.hatnoteInline (frame)
	local args = mArguments.getArgs(frame)
	local hatnote = mHatnote.hatnote(frame)
	if args.inline == nil or yesno(args.inline, true) then
		local subs = {
			['^<div'] = '<span',
			['</div>$'] = '</span>'
		}
		for k, v in pairs(subs) do hatnote = string.gsub(hatnote, k, v) end
	end
	return hatnote
end

p.hatnote = p.hatnoteInline --alias

return p