下村 一毅 |
|
Lua 的函數式編程、迭代器、產生器和組合器。以及協程的惰性求值。
Lua 的 VM 與位元組代碼。例如,作為 bind2 的實作,注入繫結值在函式的程式碼中(或許是位元組代碼和常數欄位),而不是透過包裝的匿名函式變更引數順序。
-- Lua sais: attempt to call global `fact' (a nil value) local fact = function(num) if (num > 1) return n*fact(num-1) else return 1 end end
-- this is ok. calls global 'fact' fact = function(num) if (num > 1) return n*fact(num-1) else return 1 end end
-- but since the closure depend to global, see code below temp = fact fact = function (num) return num end print(temp(10)) -- temp(10) returns 90
-- the 'a' in table is nil. local a = { a } -- what the function f returns ? ... local f = function() return f end