-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathtest.lua
75 lines (64 loc) · 1.5 KB
/
test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
local M = {}
-- Simple greeting function
function M.greeted(name)
if type(name) ~= "string" then
return "Invalid name provided"
end
return string.format("Hello, %s!", name)
end
-- Calculator function that performs basic operations
function M.calculate(operation, a, b)
if type(a) ~= "number" or type(b) ~= "number" then
return nil, "Invalid numbers provided"
end
local operations = {
add = function(x, y)
return x + y
end,
subtract = function(x, y)
return x - y
end,
multiply = function(x, y)
return x * y
end,
divide = function(x, y)
if y == 0 then
return nil, "Division by zero"
end
return x / y
end,
}
if operations[operation] then
return operations[operation](a, b)
end
return nil, "Invalid operation"
end
-- Table manipulation function
function M.filter_table(tbl, predicate)
local result = {}
for _, value in ipairs(tbl) do
if predicate(value) then
table.insert(result, value)
end
end
return result
end
-- Function with multiple returns and error handling
function M.process_data(data)
if type(data) ~= "table" then
return nil, nil, "Input must be a table"
end
local sum = 0
local count = 0
for _, value in ipairs(data) do
if type(value) == "number" then
sum = sum + value
count = count + 1
end
end
if count == 0 then
return nil, nil, "No valid numbers found"
end
return sum, sum / count, nil -- returns total, average, error
end
return M