--[[ クロージャを利用して任意函数の点 x における傾きを求めてみる。 --]] function derivative(func, delta) local delta = delta or 1e-12 return function(x) return (func(x + delta) - func(x)) / delta end end function sute(x) -- この函数の点 x における傾き (厳密解は 2 * x) を求めることにする。 return x^2 end slope = derivative(sute, _) -- これで、函数 derivative の実行結果(すなわち函数 derivative 内の無名函数) が upvalue と一緒に変数 slope に代入されたことになる。 print(slope(5))
参考: