Modul:Expr: Unterschied zwischen den Versionen

Aus skandinavien-wiki.net
(2022-08-20)
(2022-08-28)
Zeile 1: Zeile 1:
local Expr = { suite  = "Expr",
local Expr = { suite  = "Expr",
               serial = "2022-08-20",
               serial = "2022-08-28",
               item  = 54991461 }
               item  = 54991461 }
--[==[
--[==[
Zeile 27: Zeile 27:
]==]
]==]
local Failsafe = Expr
local Failsafe = Expr
if mw.site.server:find( ".beta.wmflabs.org", 4, true ) then
    require( "Module:No Globals" )
end




Zeile 43: Zeile 49:
                       ["',"]    = "[',]",
                       ["',"]    = "[',]",
                       ["'."]    = "['%.]",
                       ["'."]    = "['%.]",
                       [" "]     = " ",
                       ["U+20"]   = " ",
                       ["U+A0"]  = mw.ustring.char( 0xA0 ),
                       ["U+A0"]  = mw.ustring.char( 0xA0 ),
                       ["U+202F"] = mw.ustring.char( 0x202F ),
                       ["U+202F"] = mw.ustring.char( 0x202F ),
Zeile 95: Zeile 101:
     return r
     return r
end -- factory()
end -- factory()
local function faculty( analyze )
    -- Test for boolean interpretation
    -- Precondition:
    --    analyze  -- string or boolean or nil
    -- Postcondition:
    --    returns boolean
    local s = type( analyze )
    local r
    if s == "string" then
        r = mw.text.trim( analyze )
        if r == ""  or  r == "0"  or  r == "-" then
            r = false
        elseif r == "1" then
            r = true
        else
            r = r:lower()
            if r == "y"  or
              r == "yes"  or
              r == "true"  or
              r == "on" then
                r = true
            elseif r == "n"  or
                  r == "no"  or
                  r == "false"  or
                  r == "off" then
                r = false
            else
                if r == "falsch"  or  r == "nein" then
                    r = false
                else
                    r = true
                end
            end
        end
    elseif s == "boolean" then
        r = analyze
    elseif s == "nil" then
        r = false
    else
        r = true
    end
    return r
end -- faculty()




Zeile 172: Zeile 224:
     local r = ""
     local r = ""
     for k, v in pairs( args ) do
     for k, v in pairs( args ) do
         v = mw.text.trim( v )
         if faculty( v ) then
        if v ~= ""  and
             r = "1"
          v ~= "0" then
            break    -- for k, v
             v = v:lower( v )
            if v ~= "false"  and
              v ~= "falsch"  and
              v ~= "nein" then
                r = "1"
                break    -- for k, v
            end
         end
         end
     end    -- for k, v
     end    -- for k, v
Zeile 351: Zeile 396:


Expr.crossTotal = function ( amount )
Expr.crossTotal = function ( amount )
    -- Calculate sum of digits in integer number
    -- Precondition:
    --    amount  -- string or number, with integer
    -- Postcondition:
    --    returns number, at least 0
     local r = 0
     local r = 0
     local s = Expr.figure( amount )
     local s = Expr.figure( amount )
Zeile 361: Zeile 411:
             r = r  +  tonumber( s:sub( i, i ) )
             r = r  +  tonumber( s:sub( i, i ) )
         end -- for i
         end -- for i
    else
        r = 0
     end
     end
     return r
     return r
Zeile 376: Zeile 424:
     --    ask    -- string or not, with figure format
     --    ask    -- string or not, with figure format
     -- Postcondition:
     -- Postcondition:
     --    Returns string -- with formatted data, "0" if any problem
     --    returns mw.html -- with formatted data, or
    --              string  -- with "0" if any problem
     local r = Expr.figure( amount, ask )
     local r = Expr.figure( amount, ask )
     if r then
     if r then
Zeile 419: Zeile 468:
         :addClass( "coordinate-deg-min-sec" )
         :addClass( "coordinate-deg-min-sec" )
         e:wikitext( r )
         e:wikitext( r )
         r = tostring( e )
         r = e
     else
     else
         r = "0"
         r = "0"
Zeile 434: Zeile 483:
     --    ask    -- string, with permitted formatting, defaults to "."
     --    ask    -- string, with permitted formatting, defaults to "."
     -- Postcondition:
     -- Postcondition:
     --    Returns number, or false
     --    returns number, or false
     -- 2022-08-08
     -- 2022-08-08
     local seek = type( amount )
     local seek = type( amount )
Zeile 451: Zeile 500:
             else
             else
                 local low, split
                 local low, split
                seek = mw.text.trim( seek )
                 if seek == "" then
                 if seek == "" then
                     seek = "."
                     seek = "."
Zeile 599: Zeile 649:
     --    ask    -- string or not, with figure format
     --    ask    -- string or not, with figure format
     -- Postcondition:
     -- Postcondition:
     --    Returns number  -- with modulo remainder
     --    returns number  -- with modulo remainder
     --              0      -- if numbers are not available
     --              0      -- if numbers are not available
     local qt = Expr.figure( amount, ask )
     local qt = Expr.figure( amount, ask )
Zeile 625: Zeile 675:
     --    frame  -- object, if available
     --    frame  -- object, if available
     -- Postcondition:
     -- Postcondition:
     --    Returns string  -- with formatted percentage, terminated by %
     --    returns string  -- with formatted percentage, terminated by %
     --              0      -- if numbers are not available
     --              0      -- if numbers are not available
     local qp = Expr.figure( amount, ask )
     local qp = Expr.figure( amount, ask )
Zeile 694: Zeile 744:
     --                        or wikidata|item|~|@ or false
     --                        or wikidata|item|~|@ or false
     -- Postcondition:
     -- Postcondition:
     --    Returns string  -- with queried version/item, also if problem
     --    returns string  -- with queried version/item, also if problem
     --              false  -- if appropriate
     --              false  -- if appropriate
     -- 2020-08-17
     -- 2020-08-17
Zeile 787: Zeile 837:


function p.decimal2minsec( frame )
function p.decimal2minsec( frame )
     return Expr.decimal2minsec( frame.args[ 1 ],
     return tostring( Expr.decimal2minsec( frame.args[ 1 ],
                                frame.args[ 2 ],
                                          frame.args[ 2 ],
                                frame.args.parse )
                                          frame.args.parse ) )
end
end


Zeile 863: Zeile 913:
     end
     end
     math.randomseed( math.floor( 100000 * os.clock() ) )
     math.randomseed( math.floor( 100000 * os.clock() ) )
     return math.random( 0, n )
     return math.random( 0, n - 1 )
end
end



Version vom 28. August 2022, 20:08 Uhr

Die Dokumentation für dieses Modul kann unter Modul:Expr/doc erstellt werden

local Expr = { suite  = "Expr",
               serial = "2022-08-28",
               item   = 54991461 }
--[==[
Expr
* average
* base62
* crossTotal
* decimal2minsec
* figure
* max
* min
* minsec2decimal
* modulo
* percent
* Ramanujan
* random
* sum
* TemplateAverage
* TemplateBooland
* TemplateBoolor
* TemplateMax
* TemplateMin
* TemplateSum
* booland
* boolor
]==]
local Failsafe = Expr



if mw.site.server:find( ".beta.wmflabs.org", 4, true ) then
    require( "Module:No Globals" )
end



Expr.messagePrefix = "lua-module-Expr-"
Expr.l10nDef = {}
Expr.l10nDef[ "en" ] = {
    ErrorExpr  = "Error in mathematical expression, function#parameter"
}
Expr.l10nDef[ "de" ]  = {
    ErrorExpr  = "Fehler in mathematischem Ausdruck, Funktion#Parameter"
}
Expr.breakFigures = { [","]      = ",",
                      ["."]      = "%.",
                      ["'"]      = "'",
                      ["',"]     = "[',]",
                      ["'."]     = "['%.]",
                      ["U+20"]   = " ",
                      ["U+A0"]   = mw.ustring.char( 0xA0 ),
                      ["U+202F"] = mw.ustring.char( 0x202F ),
                      ["%s"]     = mw.ustring.char( 91, 0x20,
                                                        0xA0,
                                                        0x202F, 93 ),
                      [".%s"]    = mw.ustring.char( 91, 0x20,
                                                        0x2E,
                                                        0xA0,
                                                        0x202F, 93 ),
                      ["'%s"]    = mw.ustring.char( 91, 0x20,
                                                        0x27,
                                                        0xA0,
                                                        0x202F, 93 ),
                      ["'.%s"]   = mw.ustring.char( 91, 0x20,
                                                        0x27,
                                                        0x2E,
                                                        0xA0,
                                                        0x202F, 93 )
                    }



local function factory( say )
    -- Retrieve localized message string in content language
    -- Precondition:
    --     say  -- string, message ID
    -- Postcondition:
    --     Return some message string
    -- Uses:
    --     >  Expr.messagePrefix
    --     >  Expr.l10nDef
    --     mw.language.getContentLanguage()
    --     mw.message.new()
    local c = mw.language.getContentLanguage():getCode()
    local m = mw.message.new( Expr.messagePrefix .. say )
    local r = false
    if m:isBlank() then
        local l10n = Expr.l10nDef[ c ]
        if not l10n then
            l10n = Expr.l10nDef[ "en" ]
        end
        r = l10n[ say ]
    else
        m:inLanguage( c )
        r = m:plain()
    end
    if not r then
        r = "(((" .. say .. ")))"
    end
    return r
end -- factory()



local function faculty( analyze )
    -- Test for boolean interpretation
    -- Precondition:
    --     analyze  -- string or boolean or nil
    -- Postcondition:
    --     returns boolean
    local s = type( analyze )
    local r
    if s == "string" then
        r = mw.text.trim( analyze )
        if r == ""  or  r == "0"  or  r == "-" then
            r = false
        elseif r == "1" then
            r = true
        else
            r = r:lower()
            if r == "y"  or
               r == "yes"  or
               r == "true"  or
               r == "on" then
                r = true
            elseif r == "n"  or
                   r == "no"  or
                   r == "false"  or
                   r == "off" then
                r = false
            else
                if r == "falsch"  or  r == "nein" then
                    r = false
                else
                    r = true
                end
            end
        end
    elseif s == "boolean" then
        r = analyze
    elseif s == "nil" then
        r = false
    else
        r = true
    end
    return r
end -- faculty()



local function eval( source )
    -- Evaluate expression
    -- Precondition:
    --     source  -- string, mathematical expression
    return mw.ext.ParserFunctions.expr( source )
end -- eval()



local function expr( source, frame, show )
    -- Safe evaluation of presumable expression
    -- Precondition:
    --     source  -- string, mathematical expression
    --     frame   -- object
    --     show    -- string, details about source
    -- Postcondition:
    --     throws error, if expression failed
    --     returns number with resulting figure
    -- Uses:
    --     factory()
    local lucky, r = pcall( eval, source )
    local n = tonumber( r, 10 )
    if not ( lucky and n ) then
        r = r .. " " .. factory( "ErrorExpr" )
            .. " ''" .. show .. "'' (" .. source .. ")"
        error( r, 0 )
    else
        r = n
    end
    return r
end -- expr()



local function ellipse( a, epsilon )
    -- Circumference of an ellipse. Approximation by Ramanujan's formula.
    -- Returns the approximation and a locical value (true, if the data is well)
    epsilon = tonumber(epsilon) or false;
    a = tonumber(a) or false;
    if not epsilon then return 0, false; end
    if not a then return 0, false; end
    if epsilon < 0 or epsilon > 1 then return 0, false; end
    a = math.abs(a);

    local b = a * math.sqrt (1 - epsilon * epsilon);
    local lambda = (a - b) / (a + b);
    local circumference = math.pi * (a + b) * (1 + (3 * lambda * lambda)/(10 + math.sqrt (4 - 3 * lambda * lambda)));
    if circumference then
        return  circumference, true;
    else
        return 0, false;
    end
end



local function logicaland( args )
    local r = ""
    for k, v in pairs( args ) do
        if mw.text.trim( v ) == "" then
            r = ""
            break    -- for k, v
        else
            r = "1"
        end
    end    -- for k, v
    return r
end



local function logicalor( args )
    local r = ""
    for k, v in pairs( args ) do
        if faculty( v ) then
            r = "1"
            break    -- for k, v
        end
    end    -- for k, v
    return r
end



local function minmax( params, frame, low, lazy  )
    -- Find extremum of unnamed params values
    -- Precondition:
    --     params  -- table, like args
    --                       .minus
    --                       .zeroBlank
    --     frame   -- object
    --     low     -- true: minimum,  false: maximum
    --     lazy    -- true: try numeric result,  false: return string
    -- Postcondition:
    --     throws error, if expression failed
    --     returns number, or
    --             string if formatting required, or
    --             false if no data provided
    -- Uses:
    --     expr()
    local k, v, n, scope
    local light  = ( params.minus ~= "-" )
    local luxury = ( params.minus and light )
    local c      = mw.ustring.char( 8722 )    -- minus
    local scan   = "^%s*%-?[0-9]*%.?[0-9]*%s*$"
    local r      = false
    for k, v in pairs( params ) do
        if type( k ) == "number" then
            scope  = type( v )
            if scope == "string" then
                if v:match( "^%s*$" ) then
                    n = false
                else
                    if mw.ustring.match( v, c ) then
                        luxury = light
                        v      = mw.ustring.gsub( v, c, "-" )
                    end
                    if not mw.ustring.match( v, scan ) then
                        if low then
                            scope = "min()#"
                        else
                            scope = "max()#"
                        end
                        scope = scope .. tostring( k )
                        v     = expr( v, frame, scope )
                    end
                    n = tonumber( v )
                end
            elseif scope == "number" then
                n = v
            else
                n = false
            end
            if n then
                if r then
                    if low then
                        if n < r then
                            r = n
                        end
                    else
                        if n > r then
                            r = n
                        end
                    end
                else
                    r = n
                end
            end
        end
    end -- for k, v
    if r then
        if luxury and r < 0 then
            r = c .. tostring( -1 * r )
        elseif not lazy then
            if r == 0 then
                if params.zeroBlank then
                    r = ""
                else
                    r = "0"
                end
            else
                r = tostring( r )
            end
        end
    end
    return r
end -- minmax()



Expr.average = function ( array, ask )
    -- Calculate average
    -- Precondition:
    --     array  -- sequence table, with strings and/or numbers
    --     ask    -- string or not, with figure format
    -- Postcondition:
    --     returns number, at least 0
    local r, n = Expr.sum( array, ask )
    if n > 1 then
        r = r / n
    end
    return r
end -- Expr.average()



Expr.base62 = function ( adjust )
    -- Convert number from and to base62 encoding
    -- Precondition:
    --     adjust  -- number or ASCII string to be converted
    --                number: to base62
    --                string: base62 to number
    --     Lua limitation at 10^53; larger numbers are less precise
    -- Postcondition:
    --     returns string, or number, or false
    local r     = false
    local state = type( adjust )
    if state == "number" then
        local k = math.floor( adjust )
        if k == adjust  and  adjust > 0 then
            local m
            r = ""
            while k > 0 do
                m = k % 62
                k = ( k - m ) / 62
                if m >= 36 then
                    m = m + 61
                elseif m >= 11 then
                    m = m + 55
                else
                    m = m + 48
                end
                r = string.char( m ) .. r
            end
        elseif adjust == 0 then
            r = "0"
        end
    elseif state == "string" then
        if adjust:match( "^%w+$" ) then
            local n = #adjust
            local k = 1
            local c
            r = 0
            for i = n, 1, -1 do
                c = adjust:byte( i, i )
                if c >= 48  and  c <= 57 then
                    c = c - 48
                elseif c >= 65  and  c <= 90 then
                    c = c - 55
                elseif c >= 97  and  c <= 122 then
                    c = c - 61
                else    -- How comes?
                    r = nil
                    break    -- for i
                end
                r = r + c * k
                k = k * 62
            end -- for i
        end
    end
    return r
end -- Expr.base62()



Expr.crossTotal = function ( amount )
    -- Calculate sum of digits in integer number
    -- Precondition:
    --     amount  -- string or number, with integer
    -- Postcondition:
    --     returns number, at least 0
    local r = 0
    local s = Expr.figure( amount )
    if s then
        if s < 0 then
            s = -1 * s
        end
        s = tostring( math.floor( s ) )
        for i = 1, #s do
            r = r  +  tonumber( s:sub( i, i ) )
        end -- for i
    end
    return r
end -- Expr.crossTotal()



Expr.decimal2minsec = function ( amount, align, ask )
    -- Format coordinate value in degree, minutes, seconds
    -- Precondition:
    --     amount  -- string or number, with decimal coordinate
    --     align   -- string, number, nil, with number of decimal digits
    --     ask    -- string or not, with figure format
    -- Postcondition:
    --     returns  mw.html  -- with formatted data, or
    --              string   -- with "0" if any problem
    local r = Expr.figure( amount, ask )
    if r then
        local d = tonumber( align )
        local e = mw.html.create( "span" )
        local kd, km, low
        if r < 0 then
            low = true
            r   = -1 * r
        end
        kd = math.floor( r )
        r  = ( r - kd )  *  60
        if kd > 360 then
            kd = kd   -   math.floor( kd / 360 )  *  360
        end
        if low then
            kd = -1 * kd
        end
        km = math.floor( r )
        r  = ( r - km )  *  60
        if d  and  d >= 1  and  d < 10 then
            local s = string.format( "%%.%df", math.floor( d ) )
            local n
            r = string.format( s, r )
            n = math.floor( r )
            if r == n then
                r = tostring( n )
            else
                r = r:gsub( "^(-?%d+%.%d*[1-9])0+$", "%1" )
            end
        else
            r = tostring( math.floor( r + 0.5 ) )
        end
        if not Expr.degminsec then
            Expr.degminsec = string.format( "%%d%s %%d%s %%s%s",
                                            mw.ustring.char( 0xB0 ),
                                            mw.ustring.char( 0x2032 ),
                                            mw.ustring.char( 0x2033 ) )
        end
        r = string.format( Expr.degminsec, kd, km, r )
        e:css( "white-space", "nowrap" )
         :addClass( "coordinate-deg-min-sec" )
        e:wikitext( r )
        r = e
    else
        r = "0"
    end
    return r
end -- Expr.decimal2minsec()



Expr.figure = function ( amount, ask )
    -- Convert number from various formats
    -- Precondition:
    --     amount  -- string (or number), with number
    --     ask     -- string, with permitted formatting, defaults to "."
    -- Postcondition:
    --     returns  number, or false
    -- 2022-08-08
    local seek = type( amount )
    local r
    if seek == "string" then
        seek = ask or "."
        if type( seek ) == "string" then
            local scan = mw.text.trim( amount )
            if scan:find( "[Ee]" ) then
                scan = scan:match( "^[+%-]?([%.%d]+)[Ee][+%-]?%d+$" )
                if scan   and
                   ( scan:match( "^%.%d+$" )  or
                     scan:match( "^%d+%.?%d*$" ) ) then
                    r = tonumber( amount )
                end
            else
                local low, split
                seek = mw.text.trim( seek )
                if seek == "" then
                    seek = "."
                end
                split = seek:sub( -1 )
                seek  = seek:sub( 1, -2 )
                if seek:sub( 1, 1 ) == "-" then
                    seek = seek:sub( 2 )
                    if mw.ustring.sub( scan, 1, 1 )
                                        == mw.ustring.char( 0x2212 ) then
                        low  = true
                        scan = mw.ustring.sub( scan, 2 )
                    end
                end
                if not low then
                    if scan:sub( 1, 1 ) == "-" then
                        low  = true
                        scan = scan:sub( 2 )
                    elseif scan:sub( 1, 1 ) == "+" then
                        scan = scan:sub( 2 )
                    end
                end
                if ( split == "."  or  split == "," )   and
                   not  seek:find( split, 1, true ) then
                    local i = scan:find( split, 1, true )
                    if i then
                        split = scan:sub( i + 1 )
                        if split == "" then
                            split = false
                        end
                        if i > 1 then
                            r = scan:sub( 1,  i - 1 )
                        elseif split then
                            r = ""
                        else
                            r = false
                        end
                    else
                        split = false
                        r     = scan
                    end
                    if r then
                        seek = Expr.breakFigures[ seek ]
                        if seek then
                            local f = function ( a )
                                          local rf = a
                                          if rf:find( "&.+;" ) then
                                              rf = mw.text.decode( rf,
                                                                   true )
                                          end
                                          rf = mw.ustring.gsub( rf,
                                                                seek,
                                                                "%1%2" )
                                          return rf
                                      end
                            seek = "(%d)" .. seek .. "(%d)"
                            if r ~= "" then
                                r = f( r )
                            end
                            if split then
                                split = f( split )
                            end
                        end
                        if split  and
                           not split:match( "^%d+$" ) then
                            r = false
                        end
                        if r  and
                           not r:match( "^%d+$" ) then
                            r = false
                        end
                        if r and split then
                            r = string.format( "%s.%s", r, split )
                        end
                    end
                end
                if r then
                    r = tonumber( r )
                    if low then
                        r = -1 * r
                    end
                end
            end
        end
    elseif seek == "number" then
        r = amount
    end
    return r or false
end -- Expr.figure()



Expr.minsec2decimal = function ( aDeg, aMin, aSec, alter, ask )
    -- Convert coordinate value from degree, minutes, seconds, letter
    -- Precondition:
    --     aDeg   -- string or number, with degree
    --     aMin   -- string or number, with minutes
    --     aSec   -- string or number, with seconds
    --     alter  -- string or boolean, true|S|W, negative sign
    --     ask    -- string, with permitted formatting, defaults to "."
    local r = Expr.figure( aDeg, ask )
    if r then
        local qm = Expr.figure( aMin, ask )
        local qt = Expr.figure( aSec, ask )
        local m  = 360
        local less
        if not qt  or  qm then
            r = r  +  qm * 0.0166666666666667
            if qt then
                r = r  +  qt * 0.0002777777777777778
            end
        end
        if alter then
            local s = type( alter )
            if s == "string" then
                s = mw.text.trim( alter ):upper()
                if s == "S"  or  s == "W" then
                    less = true
                end
                if s == "N"  or  s == "S" then
                    m  = 180
                end
            elseif s == "boolean" then
                less = true
            end
        end
        if r < 0 then
            r   = -1 * r
            less = true
        end
        if r > 0 then
            r = r   -   math.floor( r / m )  *  m
        end
        if less then
            r = -1 * r
        end
    end
    return r or 0
end -- Expr.minsec2decimal()



Expr.modulo = function ( amount, adjust, ask )
    -- Retrieve modulo remainder
    -- Precondition:
    --     amount  -- string or number, with total amount (dividend)
    --     adjust  -- string or number, with modulo divisor, non-zero
    --     ask     -- string or not, with figure format
    -- Postcondition:
    --     returns  number  -- with modulo remainder
    --              0       -- if numbers are not available
    local qt = Expr.figure( amount, ask )
    local qm = Expr.figure( adjust, ask )
    local r
    if qt  and  qm  and  qm ~= 0 then
        r = qt   -   math.floor( qt / qm )  *  qm
    else
        r = 0
    end
    return r
end -- Expr.modulo()



Expr.percent = function ( amount, all, align, after, ask, allow, frame )
    -- Retrieve percentage
    -- Precondition:
    --     amount  -- string or number, with partial value
    --     all     -- string or number, with base value (100%)
    --     align   -- string, number, nil, with number of decimal digits
    --     after   -- true, if trailing zeroes shall be kept
    --     ask     -- string or not, with figure format
    --     allow   -- true, if unformatted
    --     frame   -- object, if available
    -- Postcondition:
    --     returns  string  -- with formatted percentage, terminated by %
    --              0       -- if numbers are not available
    local qp = Expr.figure( amount, ask )
    local qb = Expr.figure( all, ask )
    local r
    if qp  and  qb  and  qb ~= 0 then
        local d = tonumber( align )
        r = qp * 100 / qb
        if d  and  d >= 1  and  d < 10 then
            local s = string.format( "%%.%df", math.floor( d ) )
            s = string.format( s, r )
            if after then
                r = s
            else
                local n = math.floor( r )
                if tonumber( s ) == n then
                    r = tostring( n )
                else
                    r = s:gsub( "^(-?%d+%.%d*[1-9])0+$", "%1" )
                end
            end
        else
            r = tostring( math.floor( r + 0.5 ) )
        end
        if not allow then
            if not Expr.frame then
                Expr.frame = frame or mw.getCurrentFrame()
            end
            r = Expr.frame:callParserFunction( "formatnum", r )
        end
        r = r .. " %"
    else
        r = 0
    end
    return r
end -- Expr.percent()



Expr.sum = function ( array, ask )
    -- Calculate sum
    -- Precondition:
    --     array  -- sequence table, with strings and/or numbers
    --     ask    -- string or not, with figure format
    -- Postcondition:
    --     returns    -- 1, number, with sum, at least 0
    --                -- 2, number, of summands, at least 0
    local r1 = 0
    local r2 = 0
    if type( array ) == "table" then
        for k, v in pairs( array ) do
            v = Expr.figure( v, ask )
            if v then
                r1 = r1 + v
                r2 = r2 + 1
            end
        end -- for k, v
    end
    return r1, r2
end -- Expr.sum()



Failsafe.failsafe = function ( atleast )
    -- Retrieve versioning and check for compliance
    -- Precondition:
    --     atleast  -- string, with required version
    --                         or wikidata|item|~|@ or false
    -- Postcondition:
    --     returns  string  -- with queried version/item, also if problem
    --              false   -- if appropriate
    -- 2020-08-17
    local since  = atleast
    local last   = ( since == "~" )
    local linked = ( since == "@" )
    local link   = ( since == "item" )
    local r
    if last  or  link  or  linked  or  since == "wikidata" then
        local item = Failsafe.item
        since = false
        if type( item ) == "number"  and  item > 0 then
            local suited = string.format( "Q%d", item )
            if link then
                r = suited
            else
                local entity = mw.wikibase.getEntity( suited )
                if type( entity ) == "table" then
                    local seek = Failsafe.serialProperty or "P348"
                    local vsn  = entity:formatPropertyValues( seek )
                    if type( vsn ) == "table"  and
                       type( vsn.value ) == "string"  and
                       vsn.value ~= "" then
                        if last  and  vsn.value == Failsafe.serial then
                            r = false
                        elseif linked then
                            if mw.title.getCurrentTitle().prefixedText
                               ==  mw.wikibase.getSitelink( suited ) then
                                r = false
                            else
                                r = suited
                            end
                        else
                            r = vsn.value
                        end
                    end
                end
            end
        end
    end
    if type( r ) == "nil" then
        if not since  or  since <= Failsafe.serial then
            r = Failsafe.serial
        else
            r = false
        end
    end
    return r
end -- Failsafe.failsafe()



-- Export
local p = {}

function p.average( frame )
    local d = { }
    for k, v in pairs( frame.args ) do
        k = tostring( k )
        if k:match( "^%d+$" ) then
            table.insert( d, v )
        end
    end -- for k, v
    return Expr.average( d, frame.args.parse )
end

function p.base62( frame )
    local r
    local s = frame.args[ 1 ]
    if s then
        local s2 = frame.args[ 2 ]
        if s2 then
            s2 = mw.text.trim( s2 )
        end
        if s2 == "D2B" then
            s = tonumber( s )
        else
            s = mw.text.trim( s )
            s2 = false
        end
        r = Expr.base62( s )
        if r  and  not s2 then
            r = string.format( "%17d", r )
        end
    end
    return r or ""
end

function p.crossTotal( frame )
    return Expr.crossTotal( frame.args[ 1 ] )
end

function p.decimal2minsec( frame )
    return  tostring( Expr.decimal2minsec( frame.args[ 1 ],
                                           frame.args[ 2 ],
                                           frame.args.parse ) )
end

p.figure = function ( frame )
    return Expr.figure( frame.args[ 1 ], frame.args[ 2 ] )  or   ""
end -- p.figure

function p.max( frame )
    local lucky, r = pcall( minmax, frame.args, frame, false, false )
    return r or ""
end

function p.min( frame )
    local lucky, r = pcall( minmax, frame.args, frame, true, false )
    return r or ""
end

function p.minsec2decimal( frame )
    return Expr.minsec2decimal( frame.args[ 1 ],
                                frame.args[ 2 ],
                                frame.args[ 3 ],
                                frame.args[ 4 ],
                                frame.args.parse )
end

function p.modulo( frame )
    return Expr.modulo( frame.args[ 1 ],
                        frame.args[ 2 ],
                        frame.args[ 3 ] )
end

function p.percent( frame )
    local base = frame.args[ 2 ]
    local leave, low, pars
    if base then
        pars = frame.args
    else
        pars = frame:getParent().args
        base = pars[ 2 ]
    end
    leave = pars[ 4 ]
    if leave then
        leave = mw.text.trim( leave )
        if leave == ""  or  leave == "0" then
            leave = false
        end
    end
    low = pars.low
    if low == ""  or  low == "0" then
        low = false
    end
    return Expr.percent( pars[ 1 ], base, pars[ 3 ], leave,
                         pars.parse, low, frame )
end

function p.Ramanujan( frame )
    local semiaxis = frame.args[1] or 0;
    local eps = frame.args[2] or 0;
    local value, isOk = ellipse( semiaxis,eps );
    if isOk then
        return tostring(value);
    else
        return '<span class="error">Funktion Ramanujan in Modul Expr: ungültige Parameter!</span>'
    end
end

function p.random( frame )
    local n = Expr.figure( frame.args[ 1 ] )
    if n and n >= 2 then
        n = math.floor( n )
    else
        n = 100
    end
    math.randomseed( math.floor( 100000 * os.clock() ) )
    return math.random( 0,  n - 1 )
end

function p.sum( frame )
    local d = { }
    local r, n
    for k, v in pairs( frame.args ) do
        k = tostring( k )
        if k:match( "^%d+$" ) then
            table.insert( d, v )
        end
    end -- for k, v
    r, n = Expr.sum( d, frame.args.parse )
    return r
end

function p.TemplateAverage( frame )
    return p.average( frame:getParent() )
end

function p.TemplateBooland( frame )
    return logicaland( frame:getParent().args )
end

function p.TemplateBoolor( frame )
    return logicalor( frame:getParent().args )
end

function p.TemplateMax( frame )
    return p.max( frame:getParent() )
end

function p.TemplateMin( frame )
    return p.min( frame:getParent() )
end
function p.TemplateSum( frame )
    return p.sum( frame:getParent() )
end

function p.booland( frame )
    return logicaland( frame:getParent().args )
end

function p.boolor( frame )
    return logicalor( frame:getParent().args )
end

p.failsafe = function ( frame )
    -- Versioning interface
    local s = type( frame )
    local since
    if s == "table" then
        since = frame.args[ 1 ]
    elseif s == "string" then
        since = frame
    end
    if since then
        since = mw.text.trim( since )
        if since == "" then
            since = false
        end
    end
    return Failsafe.failsafe( since )  or  ""
end -- p.failsafe

p.Expr = function ()
    return Expr
end -- p.Expr()

setmetatable( p,  { __call = function ( func, ... )
                                 setmetatable( p, nil );
                                 return Failsafe;
                             end } );

return p