Spaces:
Paused
Paused
File size: 3,125 Bytes
93d826e | 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | -- Module definition
local Example = {}
-- Constants
local MAX_RETRIES = 3
local DEFAULT_TIMEOUT = 5000
-- Private functions (local)
local function validateInput(input)
if type(input) ~= "string" then
error("Input must be a string")
end
return true
end
-- Metatable for creating classes
local function createClass(name)
local cls = {}
cls.__index = cls
cls.__name = name
-- Constructor
function cls.new(...)
local self = setmetatable({}, cls)
if self.init then
self:init(...)
end
return self
end
return cls
end
-- Class definition using metatables
local User = createClass("User")
function User:init(name, age)
self.name = name
self.age = age
self.created_at = os.time()
end
function User:toString()
return string.format("User(%s, %d)", self.name, self.age)
end
-- Table with custom metamethods
local DataStore = {
data = {},
__newindex = function(t, k, v)
print("Setting value:", k, v)
rawset(t.data, k, v)
end,
__index = function(t, k)
return t.data[k]
end
}
setmetatable(DataStore, DataStore)
-- Coroutine example
local function producer()
return coroutine.create(function()
for i = 1, 5 do
coroutine.yield(i)
end
end)
end
-- Iterator function
local function range(from, to, step)
step = step or 1
local i = from - step
return function()
i = i + step
if i <= to then
return i
end
end
end
-- Module functions
function Example.process(input)
assert(validateInput(input))
local result = {
original = input,
processed = string.upper(input),
timestamp = os.time()
}
return result
end
-- Function with multiple returns
function Example.divide(a, b)
if b == 0 then
return nil, "Division by zero"
end
return a / b
end
-- Closure example
function Example.counter(initial)
local count = initial or 0
return function()
count = count + 1
return count
end
end
-- Table manipulation
function Example.merge(t1, t2)
local result = {}
for k, v in pairs(t1) do
result[k] = v
end
for k, v in pairs(t2) do
result[k] = v
end
return result
end
-- Pattern matching example
function Example.extractEmails(text)
local emails = {}
for email in string.gmatch(text, "[%w%.%-_]+@[%w%.%-_]+%.%w+") do
table.insert(emails, email)
end
return emails
end
-- Event handling system
local EventEmitter = createClass("EventEmitter")
function EventEmitter:init()
self.handlers = {}
end
function EventEmitter:on(event, handler)
self.handlers[event] = self.handlers[event] or {}
table.insert(self.handlers[event], handler)
end
function EventEmitter:emit(event, ...)
if self.handlers[event] then
for _, handler in ipairs(self.handlers[event]) do
handler(...)
end
end
end
-- Add classes to module
Example.User = User
Example.EventEmitter = EventEmitter
-- Module return
return Example
|