모듈:연습장/배우는사람/Bananas

local p = {}
-----------------------------------------------------------------------------------------------------------
function p.getSource(frame)
local new_args = p._getParameters( frame.args, {'source', 'target', 'start', 'plain' } ); 

return new_args'source'
end
function p.getParameters(frame)
local new_args = p._getParameters( frame.args, {'source', 'target', 'start', 'plain' } ); 

return new_args'source' .. ', ' .. new_args'target' .. ', ' .. new_args'start' .. ', ' .. new_args'plain'
end
-----------------------------------------------------------------------------------------------------------
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_argsarg
if value == nil then
value = frame_argsindex
index = index +1
end
new_argsarg = 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_argsarg
--        if value == nil then
--            value = frame_argsindex;
--            index = index + 1;
--        end
--        new_argsarg = value;
--    end
--    
--    return new_args;
--end   
-----------------------------------------------------------------------------------------------------------
function p.custom_fruit_2(frame)
local name = frame.args1
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: kiwis=5
--function p.custom_fruit_2(frame)
--	local name = frame.args1
--	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: 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.args1
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: cherries=7
--function p.has_fruit(frame)
--	local name = frame.args1
--	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: 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.args1)
local num2 = tonumber(param.args2)
return num1 + num2
end
function p.subtract(param)
local num1 = tonumber(param.args1)
local num2 = tonumber(param.args2)
return num1 - num2
end
function p.multiply(param)
local num1 = tonumber(param.args1)
local num2 = tonumber(param.args2)
return num1 * num2
end
function p.divide(param)
local num1 = tonumber(param.args1)
local num2 = tonumber(param.args2)
return num1 / num2
end
-- Two arguments, used like: 3
--function p.add(frame)
--	local num1 = tonumber(frame.args1)
--	local num2 = tonumber(frame.args2)
--	return num1 + num2
--end
-----------------------------------------------------------------------------------------------------------
function p.hello2(name)
local name1=name.args1
return "안녕하세요, " .. name1 .. "!"
end
function p.hello3(name)
local name1=name.args1
return "안녕하세요, " .. name1 .."!"
end
function p.hello4(name)
local name1=name.args1
return "안녕하세요, " .. name1.."!"
end
-- One argument, used like: Fred 
--function p.hello2(frame)
--	local name = frame.args1 -- in this example, args1 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