[์์ ]
--lexical scoping
--1๊ธ : first class citizen
-- 1๊ธ : ๊ฐ์ฒด๋ ๊ฐ ๋๋ ์ด๋ค ๊ธฐ๋ฅ ์์๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ ๊ฐ๋ฅํ๊ณ ๋ฐํ ๊ฐ๋ฅํ๋ฉฐ ๋ณ์์ ํ ๋น๋ ๊ฐ๋ฅํ ๊ฒฝ์ฐ(์คํ ์๊ฐ์ ์ฝ๋๋ก ๋ค๋ฃฐ ์ ์๋ ๊ฒ)
-- 2๊ธ : ๋งค๊ฐ๋ณ์๋ก๋ ์ ๋ฌ ๊ฐ๋ฅํ์ ๋ฐํ๊ณผ ํ ๋น์ ํ ์ ์๋ ๊ฒฝ์ฐ
-- 3๊ธ : ๋งค๊ฐ๋ณ์ ์ ๋ฌ๋ ๋ถ๊ฐ๋ฅ
-- ํจ์ํ ์ธ์ด(Function programming)
--a = {p = print}
--a.p("hello world")
--print = math.sin
--a.p(print(1)) -- equal a.p(math.sin(1))
--sin = a.p
--sin(10, 20) --equal a.p(10, 20) -> print(10, 20)
--sort
network = {
{name = "grauna", IP = "210.26.30.34"},
{name = "arraial", IP = "210.26.30.23"},
}
table.sort(network, function(a,b) return (a.name > b.name) end)
--๊ณ ์ฐจ ํจ์ : ๋ค๋ฅธ ํจ์๋ฅผ ์ธ์๋ก ๋ฐ๋ ํจ์
function derivative(f, delta)
delta = delta or 1e-4
return function(x)
return (f(x+delta) - f(x))/delta
end
end
c = derivative(math.sin)
print(math.cos(5.2), c(5.2))
print(math.cos(10), c(10))
names = {"Peter", "Paul", "Mary"}
grades = {Mary = 10, Paul = 7, Peter = 8}
print(names[1], names[2], names[3])
table.sort(names, function(n1,n2)
return grades[n1]>grades[n2]
end)
function sortbygrade (names, grades)
table.sort(names, function(n1, n2)
return grades[n1]>grades[n2]
end)
end
print(names[1], names[2], names[3])
--Closer, ์ ์ ๋ฒ์ ์ง์ . ํจ์ ์์ ํจ์์์ ์ง์ญ ๋ณ์๋ฅผ ๊ณต์ ํ๋ค.
function newCounter()
local i = 0
return function()
i = i +1
return i
end
end
c1 = newCounter()
print("c1:", c1())
print("c1:", c1())
c2 = newCounter()
print("c2:", c2())
print("c1:", c1())
print("c2:", c2())
function digitButtion(digit)
return Button{label = tostring(digit),
action = function()
add_to_display(digit)
end
}
end
--[[
oldSin = math.sin
math.sin = function(x)
return oldSin(x*math.pi/180)
end
--]]
oldSin = math.sin
do
local oldSin2 = math.sin
local k = math.pi/180
math.sin = function(x)
return oldSin2(x*k)
end
end
print(oldSin(math.pi/2)) --radian
print(math.sin(90)) --degree
-- secure environment
do
local oldOpen = io.open
local access_OK = function(filename, mode)
--access check
end
io.open = function(filename, mode)
if access_OK(filename, mode) then
return oldOpen(filename, mode)
else
return nil, "access denied"
end
end
end
Lib = {}
Lib.foo = function (x,y) return x+y end
Lib.goo = function (x,y) return x-y end
print(Lib.foo(2,3), Lib.goo(2,3))
Lib2 = {
foo2 = function (x,y) return x+y end,
goo2 = function (x,y) return x-y end
}
Lib3 = {}
function Lib3.foo(x,y) return x+y end
function Lib3.goo(x,y) return x-y end
local f = function(params)
--body
end
local g = function(params)
--body
f()
--body
end
local fact
fact = function(n)
if n == 0 then return 1
else return n*fact(n-1)
end
end
--local foo; foo = function(params) body end
local ff, gg --forward declaration
function gg()
ff()
end
function ff()
gg()
end
[๊ฒฐ๊ณผ]
0.46851667130038 0.46856084325086
-0.83907152907645 -0.83904432662041
Peter Paul Mary
Mary Peter Paul
c1: 1
c1: 2
c2: 1
c1: 3
c2: 2
1
1
5 -1
--lexical scoping
--1๊ธ : first class citizen
-- 1๊ธ : ๊ฐ์ฒด๋ ๊ฐ ๋๋ ์ด๋ค ๊ธฐ๋ฅ ์์๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ ๊ฐ๋ฅํ๊ณ ๋ฐํ ๊ฐ๋ฅํ๋ฉฐ ๋ณ์์ ํ ๋น๋ ๊ฐ๋ฅํ ๊ฒฝ์ฐ(์คํ ์๊ฐ์ ์ฝ๋๋ก ๋ค๋ฃฐ ์ ์๋ ๊ฒ)
-- 2๊ธ : ๋งค๊ฐ๋ณ์๋ก๋ ์ ๋ฌ ๊ฐ๋ฅํ์ ๋ฐํ๊ณผ ํ ๋น์ ํ ์ ์๋ ๊ฒฝ์ฐ
-- 3๊ธ : ๋งค๊ฐ๋ณ์ ์ ๋ฌ๋ ๋ถ๊ฐ๋ฅ
-- ํจ์ํ ์ธ์ด(Function programming)
--a = {p = print}
--a.p("hello world")
--print = math.sin
--a.p(print(1)) -- equal a.p(math.sin(1))
--sin = a.p
--sin(10, 20) --equal a.p(10, 20) -> print(10, 20)
--sort
network = {
{name = "grauna", IP = "210.26.30.34"},
{name = "arraial", IP = "210.26.30.23"},
}
table.sort(network, function(a,b) return (a.name > b.name) end)
--๊ณ ์ฐจ ํจ์ : ๋ค๋ฅธ ํจ์๋ฅผ ์ธ์๋ก ๋ฐ๋ ํจ์
function derivative(f, delta)
delta = delta or 1e-4
return function(x)
return (f(x+delta) - f(x))/delta
end
end
c = derivative(math.sin)
print(math.cos(5.2), c(5.2))
print(math.cos(10), c(10))
names = {"Peter", "Paul", "Mary"}
grades = {Mary = 10, Paul = 7, Peter = 8}
print(names[1], names[2], names[3])
table.sort(names, function(n1,n2)
return grades[n1]>grades[n2]
end)
function sortbygrade (names, grades)
table.sort(names, function(n1, n2)
return grades[n1]>grades[n2]
end)
end
print(names[1], names[2], names[3])
--Closer, ์ ์ ๋ฒ์ ์ง์ . ํจ์ ์์ ํจ์์์ ์ง์ญ ๋ณ์๋ฅผ ๊ณต์ ํ๋ค.
function newCounter()
local i = 0
return function()
i = i +1
return i
end
end
c1 = newCounter()
print("c1:", c1())
print("c1:", c1())
c2 = newCounter()
print("c2:", c2())
print("c1:", c1())
print("c2:", c2())
function digitButtion(digit)
return Button{label = tostring(digit),
action = function()
add_to_display(digit)
end
}
end
--[[
oldSin = math.sin
math.sin = function(x)
return oldSin(x*math.pi/180)
end
--]]
oldSin = math.sin
do
local oldSin2 = math.sin
local k = math.pi/180
math.sin = function(x)
return oldSin2(x*k)
end
end
print(oldSin(math.pi/2)) --radian
print(math.sin(90)) --degree
-- secure environment
do
local oldOpen = io.open
local access_OK = function(filename, mode)
--access check
end
io.open = function(filename, mode)
if access_OK(filename, mode) then
return oldOpen(filename, mode)
else
return nil, "access denied"
end
end
end
Lib = {}
Lib.foo = function (x,y) return x+y end
Lib.goo = function (x,y) return x-y end
print(Lib.foo(2,3), Lib.goo(2,3))
Lib2 = {
foo2 = function (x,y) return x+y end,
goo2 = function (x,y) return x-y end
}
Lib3 = {}
function Lib3.foo(x,y) return x+y end
function Lib3.goo(x,y) return x-y end
local f = function(params)
--body
end
local g = function(params)
--body
f()
--body
end
local fact
fact = function(n)
if n == 0 then return 1
else return n*fact(n-1)
end
end
--local foo; foo = function(params) body end
local ff, gg --forward declaration
function gg()
ff()
end
function ff()
gg()
end
[๊ฒฐ๊ณผ]
0.46851667130038 0.46856084325086
-0.83907152907645 -0.83904432662041
Peter Paul Mary
Mary Peter Paul
c1: 1
c1: 2
c2: 1
c1: 3
c2: 2
1
1
5 -1
[LUA] Chap6. internal of Function
Reviewed by kukanuc
on
3์ 10, 2019
Rating:
๋๊ธ ์์: