定义:一个函数能够嵌套在另外一个函数中,内部函数能够访问外部函数定义的局部变量函数
函数与其余全部的值同样都是匿名的,没有名称lua
讨论print()
函数时,至关于在讨论值仅为 print()
的变量code
a = {p = print} a.p("Hello World") -- Hello World c = print c("eee") -- eee print = math.sin a.p(print(math.pi / 2)) -- 1 sin = a.p sin(10, 20) -- 10 20 function foo(x) return 2 * x end -- 等价于 foo = function (x) return 2 * x end
function foo(x) return 2 * x end -- 等价于 foo = function(x) return 2 * x end
table.sort()
这样的函数接收另外一个函数做为实参的函数就称它为高阶函数-- table.sort 对 table 中的全部元素进行排序 network = { {name = "atest", IP = "255.255.255.0",} {name = "btest2"}, IP = "255.255.255.1"}, {name = "ctest3"}, IP = "255.255.255.3"}, {name = "dtest4"}, IP = "255.255.255.4"}, } table.sort(network, function (a, b) return (a.name > b. name) end ) -- 导数 (f(x + d) - f(x)) / d 函数在某一个点处的导数 function derivative (f, delta) delta = delta or 1e-4 return function (x) return (f(x + delta) - f(x)) / delta end end -- sin 的导数是 cos c = derivative(math.sin) print(math.sin(1), c(1)) print(math.sin(1), math.cos(0))
names = {"Pe", "Tu", "Me"} grades = {Pe = 10, Tu = 5, Me = 8} table.sort(names, function (x, y) return grades[x] > grades[y] end ) -- 按照年级进行排序 print(names[1], names[2], names[3]) function sortByGrade(names, grades) table.sort(names, function (x, y) return grades[x] > grades[y] end ) end sortByGrade(names, grades)
closure
就是一个一个函数加上该函数所需访问的全部「非局部变量」newCounter
,那么他会建立一个新的局部变量 i
从而也会获得一个新的 closure
function newCounter() local i = 0 return function () i = i + 1 return i end end c1 = newCounter() print(c1()) -- 1 print(c1()) -- 2 -- 从新定义 sin 函数,弧度转为角度 oldSin = math.sin math.sin = function (x) return oldSin(x * math.pi / 180) end do local oldSin = math.sin local k = math.pi / 180 math.sin = function (x) return oldSin(x * k) end end -- 限制一个程序访问文件 do local oldOpen = io.open local access_OK = function (filename, mode) -- <检查访问权限的代码> end io.open = function (filename, mode) if access_OK(filename, mode) then print("容许访问") return oldOpen(filename, mode) else print("不容许访问") return nil, "access denied" end end end