An unofficial, read-only Hacker News reader.

Lua for data representation (2025) (opens external site)

104 points by vinhnx 4 days ago | citrons.xyz | 61 comments on HN

View full thread · View on Hacker News

Comment thread (6)

  1. Also, it’s possible to force global variables to be explicitly declared.

      function declare(name)
        rawset(_G,name,false)
      end
      function exists(name)
        if rawget(_G,name) ~= nil then 
          return true 
        else return false end
      end
      setmetatable(_G, {
        __newindex = function(self,x) error("Undeclared global " .. x) end,
        __index = function(self,x) error("Undeclared global " .. x) end
      })
    
    And now

      declare("foo")
      foo = 1
      -- Create name if it hasn’t 
      -- been declared yet
      if not exists("name") then 
        declare("name")
        name = 1
      end
    
    More discussion: https://lua.org/pil/14.2.html
    Replies (1)
    1. That is neat how that's possible to do! It would be nice if it would just do local by default as well. Basically swap the defaults: everything is local and only what's explicitly global is global? I realize it's not an easy thing to change at this point in the game. Maybe some compile pragma or another extension could help? But it should be a new default in a modern language.
      Replies (1)
      1. The reason I put up with things like global by default, no “continue”, “--” instead of “#” for comments, and so on is because Lua is widely implemented, to the point it’s an informal standard for embedded scripting. There’s PUC Lua (the original), LuaJIT, Lua in Rust, Lua in Go, Lua in C#, Lua in Javascript, Lua that compiles to native code, and so on. All of these variants have agreed on having Lua, flaws and all.

        I would love to see a POSIX standard for Lua (probably based on Lua 5.1); it’s widely enough deployed that a standard for it would make sense. Right now the standard is either PUC Lua 5.1 or whatever the latest release of PUA Lua is at a given time.

        I prefer Lua over Perl/Python/Ruby because I know I can get a Lua runtime going in whatever environment I’m in—e.g. when writing my Lua book, I used my personal fork of Lua 5.1, but the person I was writing the book with was using Moonsharp (Lua in C#) since they were a C# developer. We could easily share code with each other.

        Replies (1)
        1. Interested in your lua book, is it available now?
          Replies (1)
          1. Yes! It’s public domain (especially the code examples), and here is the PDF:

            https://samboy.github.io/lunacy-book.pdf

            It’s a book for someone already familiar with any of AWK/Perl/Python who wants to use Lua mainly for text and regular expression parsing.

            The book covers a lot of bits of Lua which are easy to do in Perl/Python but hard to do in Lua. For example, here’s a Lua version of `deepcopy`:

              function deepcopy(orig, seen)
                if not seen then
                  seen = {}
                end
                if type(orig) ~= "table" then
                  return orig
                end
                local out = {}
                seen[orig] = out
                for k,v in pairs(orig) do
                  if not seen[v] then
                    out[k] = deepcopy(v, seen)
                  else
                    out[k] = seen[v]
                  end
                end
                return out
              end
            
            Likewise, here’s Lua’s take on returning the keys of a table in order:

              -- This code is public domain
              -- Like pairs() but sorted
              function sPairs(inTable, sFunc)
                if not sFunc then
                  sFunc = function(a, b)
                    local ta = type(a)
                    local tb = type(b)
                    if(ta == tb) then
                      if ta == 'number'
                        then return a < b
                      end
                      return tostring(a) <
                             tostring(b)
                    end
                    return ta < tb
                  end
                end
                local keyList = {}
                local index = 1
                for k,_ in pairs(inTable) do
                  table.insert(keyList,k)
                end
                table.sort(keyList, sFunc)
                return function()
                  key = keyList[index]
                  index = index + 1
                  return key, inTable[key]
                end
              end
            
            And, finally, a lua version of `split` from Perl/Python:

              function split(s, splitOn)
                if not splitOn then splitOn = "," end
                local place = true
                local out = {}
                local mark
                local last = 1
                while place do
                  place, mark = string.find(s, splitOn, last, false)
                  if place then
                    table.insert(out,string.sub(s, last, place - 1))
                    last = mark + 1
                  end
                end
                table.insert(out,string.sub(s, last, -1))
                return out
              end
            
            (All three of these functions are public domain)
            Replies (1)
            1. thanks, reading on my kindle now!