모듈:연습장/배우는사람/Bananas: 두 판 사이의 차이

위키백과, 우리 모두의 백과사전.
내용 삭제됨 내용 추가됨
편집 요약 없음
편집 요약 없음
1번째 줄: 1번째 줄:
local p = {}
local p = {}



-----------------------------------------------------------------------------------------------------------
function p._getParameters(frame_args, arg_list)
local new_args = {};
local index = 1;
local value;

for i, arg in ipairs(arg_list) do
value = frame_args[arg]
if value == nil then
value = frame_args[index]
index = index +1
end
new_args[arg] = value;
end

return new_args;
end

--[[
Helper function that populates the argument list given that user may need to use a mix of
named and unnamed parameters. This is relevant because named parameters are not
identical to unnamed parameters due to string trimming, and when dealing with strings
we sometimes want to either preserve or remove that whitespace depending on the application.
]]
--function str._getParameters( frame_args, arg_list )
-- local new_args = {};
-- local index = 1;
-- local value;
--
-- for i,arg in ipairs( arg_list ) do
-- value = frame_args[arg]
-- if value == nil then
-- value = frame_args[index];
-- index = index + 1;
-- end
-- new_args[arg] = value;
-- end
--
-- return new_args;
--end


-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------

2018년 5월 23일 (수) 14:05 판

local p = {}


-----------------------------------------------------------------------------------------------------------
function p._getParameters(frame_args, arg_list)
    local new_args = {};
    local index = 1;
    local value;

    for i, arg in ipairs(arg_list) do
       value = frame_args[arg]
       if value == nil then
          value = frame_args[index]
          index = index +1
       end
       new_args[arg] = value;
    end

    return new_args;
end

--[[
Helper function that populates the argument list given that user may need to use a mix of
named and unnamed parameters.  This is relevant because named parameters are not
identical to unnamed parameters due to string trimming, and when dealing with strings
we sometimes want to either preserve or remove that whitespace depending on the application.
]]
--function str._getParameters( frame_args, arg_list )
--    local new_args = {};
--    local index = 1;
--    local value;
--    
--    for i,arg in ipairs( arg_list ) do
--        value = frame_args[arg]
--        if value == nil then
--            value = frame_args[index];
--            index = index + 1;
--        end
--        new_args[arg] = value;
--    end
--    
--    return new_args;
--end   

-----------------------------------------------------------------------------------------------------------
function p.custom_fruit_2(frame)
    local name = frame.args[1]
    local result = name .. ' has:'
    local count = 0
    for name, value in pairs(frame.args) do
        if name ~= 1 then
            count = count + 1
            if count == 1 then
                result = result .. ' ' .. value .. ' ' .. name
            else
                result = result .. ', ' .. value .. ' ' .. name
            end
        end 
    end
    return result
end


-- Iterating over args with separate mandatory args
-- Used like: {{#invoke:BananasArgs|custom_fruit_2|Fred|pineapples=10|kiwis=5}}
--function p.custom_fruit_2(frame)
--	local name = frame.args[1]
--	local result = name .. ' has:'
--	for name, value in pairs(frame.args) do
--		if name ~= 1 then
--			result = result .. ' ' .. value .. ' ' .. name
--		end
--	end
--	return result
--end

-----------------------------------------------------------------------------------------------------------
function p.custom_fruit(frame)
    local result = 'I have:'
    for name, value in pairs(frame.args) do
        if result == 'I have:' then
            result = result .. ' ' .. value .. ' ' .. name
        else
            result = result .. ', ' .. value .. ' ' .. name
        end
    end
    return result
end

-- Iterating over args, used like: {{#invoke:BananasArgs|custom_fruit|pineapples=10|kiwis=5}}
--function p.custom_fruit(frame)
--	local result = 'I have:'
--	for name, value in pairs(frame.args) do
--		result = result .. ' ' .. value .. ' ' .. name
--	end
--	return result
--end

-----------------------------------------------------------------------------------------------------------
function p.has_fruit(param)
    local name = param.args[1]
    local num_bananas = param.args.bananas
    local num_apples = param.args.apples
    local num_cherries = param.args.cherries

    if not name then return '' end
    local result = name .. ' has'
    if num_bananas then 
        result = result .. ' ' .. num_bananas .. ' bananas' 
    end
    if num_apples then 
       if num_bananas then   
         result = result .. ', ' .. num_apples .. ' apples' 
       else
          result = result .. ' ' .. num_apples .. ' apples' 
        end
    end
    if num_cherries then 
        if num_bananas or num_apples then 
             result = result .. ', ' .. num_cherries.. ' cherries' 
        else
             result = result .. ' ' .. num_cherries.. ' cherries' 
        end
    end
    return result
end

-- Mixing regular args with named args and optional named args
-- Used like: {{#invoke:BananasArgs|has_fruit|Fred|bananas=5|cherries=7}}
--function p.has_fruit(frame)
--	local name = frame.args[1]
--	local num_bananas = frame.args.bananas
--	local num_apples = frame.args.apples
--	local num_cherries = frame.args.cherries
--	
--	if not name then return '' end
--	local result = name .. ' has:'
--	if num_bananas then result = result .. ' ' .. num_bananas .. ' bananas' end
--	if num_apples then result = result .. ' ' .. num_apples .. ' apples' end
--	if num_cherries then result = result .. ' ' .. num_cherries .. ' cherries' end
--	return result
--end
-----------------------------------------------------------------------------------------------------------
function p.count_fruit(param)
   local num_bananas = param.args.bananas
   local num_apples = param.args.apples
   local num_pears = param.args.pears

  return 'There are ' .. num_bananas .. ' bananas, ' .. num_apples .. ' apples, and ' .. num_pears .. ' pears.'  
end

-- Named arguments, used like: {{#invoke:BananasArgs|count_fruit|bananas=5|apples=3}}
--function p.count_fruit(frame)
--	local num_bananas = frame.args.bananas
--	local num_apples = frame.args.apples
--	return 'I have ' .. num_bananas .. ' bananas and ' .. num_apples .. ' apples'
--end

-----------------------------------------------------------------------------------------------------------

function p.add(param)
    local num1 = tonumber(param.args[1])
    local num2 = tonumber(param.args[2])
    return num1 + num2
end

function p.subtract(param)
    local num1 = tonumber(param.args[1])
    local num2 = tonumber(param.args[2])
    return num1 - num2
end

function p.multiply(param)
    local num1 = tonumber(param.args[1])
    local num2 = tonumber(param.args[2])
    return num1 * num2
end

function p.divide(param)
    local num1 = tonumber(param.args[1])
    local num2 = tonumber(param.args[2])
    return num1 / num2
end


-- Two arguments, used like: {{#invoke:BananasArgs|add|5|3}}
--function p.add(frame)
--	local num1 = tonumber(frame.args[1])
--	local num2 = tonumber(frame.args[2])
--	return num1 + num2
--end

-----------------------------------------------------------------------------------------------------------

function p.hello2(name)
   local name1=name.args[1]
   return "안녕하세요, " .. name1 .. "!"
end

function p.hello3(name)
  local name1=name.args[1]
   return "안녕하세요, " .. name1 .."!"
end

function p.hello4(name)
   local name1=name.args[1]
   return "안녕하세요, " .. name1.."!"
end

-- One argument, used like: {{#invoke:BananasArgs|hello|Fred}} 
--function p.hello2(frame)
--	local name = frame.args[1] -- in this example, args[1] is the word Fred 
--	return "Hello, " .. name .. "!" -- .. name .. replace by the word Fred
--end

-----------------------------------------------------------------------------------------------------------

function p.hello()
   return "안녕하세요, 루아입니다."
end

return p

-- 헬로 월드!
-- local p = {}
 
-- function p.hello()
--    return "Hello, world!"
--end
 
--return p