kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

hello.lua (1151B)


      1 -- Deterministic Lua script run by the interpreter built from the Lua sources.
      2 -- Exercises tables, string formatting, closures, coroutines, pcall, and math.
      3 
      4 -- string.format goes through the host snprintf; fixed values keep it stable.
      5 print(string.format("fmt %d %.3f %s", 42, 2.5, ("ab"):rep(3)))
      6 
      7 -- table build + sort + concat
      8 local t = {}
      9 for i = 1, 8 do t[i] = (i * 7) % 11 end
     10 table.sort(t)
     11 print("sorted: " .. table.concat(t, ","))
     12 
     13 -- closure / counter
     14 local function counter()
     15   local n = 0
     16   return function() n = n + 1; return n end
     17 end
     18 local c = counter()
     19 print("counter: " .. c() .. c() .. c())
     20 
     21 -- coroutine producing a fixed sequence
     22 local co = coroutine.wrap(function()
     23   for i = 1, 4 do coroutine.yield(i * i) end
     24 end)
     25 print("coro: " .. co() .. "," .. co() .. "," .. co() .. "," .. co())
     26 
     27 -- protected call over an error
     28 local ok, err = pcall(function() error("boom") end)
     29 print("pcall: " .. tostring(ok) .. " " .. (err:match("boom") or "?"))
     30 
     31 -- integer / float math (Lua 5.4 has a real integer subtype)
     32 print(string.format("math %d %d %.4f", 7 // 2, 2 ^ 10, math.sqrt(2)))
     33 print("type: " .. math.type(3) .. "/" .. math.type(3.0))