Skip to content

Commit afbeb18

Browse files
Added some built-in functions
1 parent d53c551 commit afbeb18

File tree

1 file changed

+63
-10
lines changed

1 file changed

+63
-10
lines changed

pythonlua/luainit.lua

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,70 @@ string_meta.__add = function(v1, v2)
1010
return v1 + v2
1111
end
1212

13-
local str = tostring
13+
local g_real_unpack = unpack or table.unpack
14+
15+
local unpack = function(t)
16+
if type(t) == "table" and t.is_list then
17+
return g_real_unpack(t._data)
18+
end
19+
return g_real_unpack(t)
20+
end
21+
22+
local abs = math.abs
23+
local ascii = string.byte
24+
local chr = string.char
1425
local int = tonumber
26+
local str = tostring
27+
28+
local function all(iterable)
29+
for element in iterable do
30+
if not element then
31+
return false
32+
end
33+
end
34+
return true
35+
end
36+
37+
local function any(iterable)
38+
for element in iterable do
39+
if element then
40+
return true
41+
end
42+
end
43+
return false
44+
end
45+
46+
local function bool(x)
47+
if x == false or x == nil or x == 0 then
48+
return false
49+
end
50+
51+
if type(x) == "table" then
52+
if x.is_list or x.is_dict then
53+
return next(x._data) ~= nil
54+
end
55+
end
56+
57+
return true
58+
end
59+
60+
local function callable(x)
61+
local x_type = type(x)
62+
if x_type == "function" then
63+
return true
64+
end
65+
if x_type == "table" then
66+
local meta = getmetatable(x)
67+
return type(meta.__call) == "function"
68+
end
69+
70+
return false
71+
end
72+
73+
local function divmod(a, b)
74+
local res = { math.floor(a / b), math.fmod(a, b) }
75+
return unpack(res)
76+
end
1577

1678
local function len(t)
1779
if type(t._data) == "table" then
@@ -65,15 +127,6 @@ local function enumerate(t, start)
65127
end
66128
end
67129

68-
local g_real_unpack = unpack or table.unpack
69-
70-
local unpack = function(t)
71-
if type(t) == "table" and t.is_list then
72-
return g_real_unpack(t._data)
73-
end
74-
return g_real_unpack(t)
75-
end
76-
77130
local list = {}
78131
setmetatable(list, {
79132
__call = function(_, t)

0 commit comments

Comments
 (0)