lua-users home
lua-l archive

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


> Not terribly hard to do, but ...
>
>        function foo()
>          local v = { 1 , 2 , 3 , 4 , 5 }
>          local a = 4
>          local f = h"|x| x * a + 2"
>          local r = map(f,v)
>
>          map(h"|x| print(x)",r)
>        end
>
>        foo()
>        [string "return function(x) return  x * a + 2 end"]:1: attempt to perform
>        arithmetic on global 'a' (a nil value) stack traceback:
>          [string "return function(x) return  x * a + 2 end"]:1: in function 'f'
>          stdin:4: in function 'map'
>          stdin:5: in function 'foo'
>          stdin:1: in main chunk
>          [C]: ?
>
> So it's not perfect.  But hey, it was what?  All of ten minutes or so?
>
>  -spc (It was easier to write than I thought actually ... )
>
>

That's been what I've been meaning with

"Of course, you lose local
scoped variables ... but for your cause, it could help. Further
transformations could be to insert values from variables via table
declaration, but that might be overly confusing."

There's a fundamental difference between local values and global
values, and the best you can get from a loadstring function is access
to "global" variables that are then mapped onto local variables -
which is cumbersome. For example, you could set a function environment
onto the "lambda" function, and any variable access onto globals is
tried to be looked up in the local variables of the creating function
(though that would be tricky, yet it's doable). But I would say that
this is not the way to go. There's the other discussion about a "short
function syntax".... but I am not sure if that's really what we need
and want. I use a lot of closures in my code, sometimes I also wished
for a shorter syntax and yet - it's ok the way it is, even if it takes
more writing:

...:filter(function(x) return x<0 end)
vs.
...:filter "(x)=x<0"

But anyway, that's not really related to the "underscore" project anymore.

Eike