lua-users home
lua-l archive

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


On Sat, Oct 30, 2010 at 6:24 AM, Phil Bewig <pbewig@gmail.com> wrote:
> Scheme provides a delay function.  You can see an implementation at SRFI-45,
> and some uses of that delay function at SRFI-41.  Even if the Scheme code
> isn't directly portable to Lua, the explanations should give you some good
> ideas.
> Phil
>

http://github.com/archilifelin/mysandbox/blob/master/lua/lazy.lua

I tried my best for now : )

After doing some researches, I guess the easiest way to implement
something works like lazy eval in Lua is still using short anonymous
functions (string lambdas), which can be compiled on the fly. I still
used a table to inject force() on the anonymous function automatically
when the table __call is invoked.

And the anonymous function's env is still needed to be supplied manually, e.g.
-- there is some x out here
lazy("x(x)", {x=x})

I found a hacky way to get upvalues on stackoverflow (the page link is
in the lazy.lua), however it's using debug module which I don't
consider a formal way.

There's still some possibility to write simply lazy( x(x) ) without
the real function being evaluated, and then I even don't need to
supplement the env by hand, I guess. But then the x will not be a
plain Lua function, it must be some kind of object(table) that
encapsulated the real function and do lots of complex stuff behind the
scene. That's beyond my ability for now. : )