Shimomura Ikkei |
|
Lua中的函数式编程、迭代器、生成器、组合子。以及使用协程实现惰性求值。
Lua的虚拟机和字节码。例如,作为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