• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Search results

  1. GavinW

    Lua compiler

    Google srlua.
  2. GavinW

    Help with this function

    By definition of the colon operator, s:sub(x,x+y) is s.sub(s,x,x+y), which by definition of the dot operator is s["sub"](s, x,x+y). But if s is a string we have to look up the value of s["sub"] in the __index field of s's metatable. This is the same as the metatable of the table called...
  3. GavinW

    Print table at specific table depth

    local scope scope = function (t, level) if level == 1 then for _, v in pairs (t) do if type (v) == "string" then print (v) end -- if end -- for return else for _, v in pairs (t) do if type (v) == "table" then scope (v, level - 1) end -- if end --...
  4. GavinW

    How come there are no table methods?

    t = setmetatable ({ }, { __index = table }) t:insert "hello" print (t[1]) --> hello My guess is that tables, being the sole datatype in Lua, are left in their pristine unmetatabled form so as not to foist upon users what they may not want. If only supermarkets would follow this principle and...
  5. GavinW

    Help Lua code

    Looks like you made two typos and upped the case of 'l' and 'i' in function luai_244288.
  6. GavinW

    How works _G and _ENV and what can I do with them?

    Herly, I recommend that you buy Programming in Lua (Fourth Edition) by Roberto Ierusalimschy, if you do not yet have it. You won't regret it. Chapter 22 (The Environment) is what you need. All variables in Lua are either keys in a table or are local to a chunk. So-called global variables are...
  7. GavinW

    Can an anonymus function call itself?

    Thanks. I thought it was something like that. My personal preference for the unsugared syntax is because the sugar seems to me to be unnecessarily apologetic. It is just because many Lua users come from a first-order background, rather than a higher-order one, that they should perhaps be...
  8. GavinW

    Can an anonymus function call itself?

    The Y combinator as given above provides a means of doing this. Its crucial property is that Y(f) reduces to f(Y(f)) - the fixed-point property. Using it makes it possible to rewrite definitions without explicit recursion. Thus, in the remarks above, Y(F) does the same as fact. You get F from...
  9. GavinW

    Can an anonymus function call itself?

    OK. The factorial function imay be given by (function (g) return ( function (f) f = g ( function (x) return (f (x)) end) return f end) end ) ( function (h) return function (n) return (n<2) and n or n*h(n-1) end end)There are no named functions here. But naming is only a way of making...
  10. GavinW

    Can an anonymus function call itself?

    It looks like my first suggestion does not work. I think what you are asking for is how to do a calculation likefunction fact (n) return (n<2) and n or n*fact(n-1) end print (fact(4)) --> 24without using a recursive function definition. What about this?local Y = function (g) return (function...
  11. GavinW

    Can an anonymus function call itself?

    You could use a fixed-point combinator. For example Y = function (f) local g = function (x) return f ( x (x)) end return g(g) end
  12. GavinW

    How does an operation that relates to a value relate to a table that is in order.

    t = { val = 3, func = function (self) print (self.val) end, } t:func ( ) -- > 3
  13. GavinW

    Sets please

    If you find yourself writing a program with sets (as opposed to ordered sets) in it, then you probably have the wrong algorithm.
  14. GavinW

    Lua 'end' syntax

    comment For the sake of two hyphens you can have your cake and eat it end -- comment
  15. GavinW

    How to loadstring()?

    You cannot. In Lua 5.2 loadstring is replaced by load which can take an environment as its fourth argument, but that will not help you with using a local variable.
  16. GavinW

    metatables do not work!

    Change the first line from local CLASS = { to local CLASS; CLASS = { The variable CLASS is not in scope when you use it in line 6.
  17. GavinW

    How would I create a Lua interface so the user can write his/her code in a startup() method and a runloop() method

    Forgive me, but I find this very opaque. Finish what? To do the same as what? Please remember that us mortals are not telepathic. I use an operating system in which the window manager is the task-manager and user-applications have to co-operate with it if they want to use the graphical user...
  18. GavinW

    How do i overwrite an variable inside an if loop

    What about this? function (xmid, im) local top_margin = 1 for y = 0, xhalf do local c1 = im.get_pixel (xmid, y) if c1.alpha < 20 then top_margin = y + 1 end -- if end -- for return top_margin end -- function
  19. GavinW

    Roblox How to use tonumber, but with text instead of numbers?

    Use a local variable? Look up automagic on the Lua wiki?
  20. GavinW


    A friend has pointed out where uname appears in the makefile. It comes in a swathe of stuff to determine what platform is being used, so I can simply comment it all out and insert the answer. This exposes the next layer of the problem for me: the confusion caused by the overlapping of...