lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


You can simulate some of the features of lazy evaluation in lua, such
using metatables.

For example, you can simulate infinite lists, who's entries are only
evaluated when they are accessed.
The wikipedia article on lua shows such a table for all fibonacci numbers:


fibs = { 1, 1 }                                -- Initial values for
fibs[1] and fibs[2].
setmetatable(fibs, {                           -- Give fibs some magic behavior.
   __index = function(fibs, n)                -- Call this function
if fibs[n] does not exist.
       fibs[n] = fibs[n - 1] + fibs[n - 2]    -- Calculate and memoize fibs[n].
       return fibs[n]
   end
})

http://en.wikipedia.org/wiki/Lua_programming_language