lua-users home
lua-l archive

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


On 1/11/2010, at 5:39 AM, Johnson Lin wrote:

> 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. : )

I'm not sure it's quite what you want, but with Rima [1] you can:

local f, x = rima.R"f, x"
local exp = f(x)
print(rima.E(exp, {f=math.sqrt, y=100}) --> 10

One difference being that in Rima, supplying the env manually is a feature.

Cheers,
Geoff

[1] http://code.google.com/p/rima