-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.lua
165 lines (130 loc) · 3.88 KB
/
complex.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
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
159
160
161
162
163
164
165
-- Metatable example: Vector operations
Vector = {}
Vector.__index = Vector
function Vector.new(x, y, z)
local vec = {x = x or 0, y = y or 0, z = z or 0}
setmetatable(vec, Vector)
return vec
end
function Vector:__add(other)
return Vector.new(self.x + other.x, self.y + other.y, self.z + other.z)
end
function Vector:__sub(other)
return Vector.new(self.x - other.x, self.y - other.y, self.z - other.z)
end
function Vector:__tostring()
return "(" .. self.x .. ", " .. self.y .. ", " .. self.z .. ")"
end
function Vector:__call()
print( "Vector called with: ", self )
end
-- Using the vector class
local v1 = Vector.new(1, 2, 3)
local v2 = Vector.new(4, 5, 6)
local v3 = v1 + v2
print("Vector Addition: " .. tostring(v3))
local v4 = v2 - v1
print("Vector Subtraction: " .. tostring(v4))
v1()
-- Closure example: Counter
function create_counter(start)
local count = start or 0
return function(increment)
count = count + (increment or 1)
return count
end
end
local counter1 = create_counter(10)
local counter2 = create_counter(100)
print("Counter 1:", counter1(2)) -- 12
print("Counter 1:", counter1(3)) -- 15
print("Counter 2:", counter2(5)) -- 105
print("Counter 2:", counter2(10)) -- 115
-- Table manipulation and recursive function example: Merge Sort
function merge_sort(arr)
if #arr <= 1 then
return arr
end
local mid = math.floor(#arr / 2)
local left = merge_sort({table.unpack(arr, 1, mid)})
local right = merge_sort({table.unpack(arr, mid + 1, #arr)})
return merge(left, right)
end
function merge(left, right)
local result = {}
local i, j = 1, 1
while i <= #left and j <= #right do
if left[i] < right[j] then
table.insert(result, left[i])
i = i + 1
else
table.insert(result, right[j])
j = j + 1
end
end
while i <= #left do
table.insert(result, left[i])
i = i + 1
end
while j <= #right do
table.insert(result, right[j])
j = j + 1
end
return result
end
-- Test merge sort
local unsorted = {38, 27, 43, 3, 9, 82, 10}
local sorted = merge_sort(unsorted)
print("Sorted Array:")
print( table.concat(sorted, ", ") )
for idx, v in ipairs(sorted) do
print(idx, v)
end
-- Coroutine example: Fibonacci generator
function fibonacci()
local a, b = 0, 1
return coroutine.create(function()
while true do
if a > 100 then
return "Done"
end
coroutine.yield(a)
a, b = b, a + b
end
end)
end
print( 'Fibonacci numbers with coroutine:' )
local fib = fibonacci()
while true do
local success, value = coroutine.resume(fib)
if success == false then
print("Error: " .. value)
break
else
print("Fibonacci number " .. tostring(success) .. ", " .. ": " .. value)
end
end
print( 'coroutine.wrap test' )
w = coroutine.wrap( function()
for i = 1, 10 do
coroutine.yield( i )
end
return 100
end )
for i = 1, 12 do
print( w() )
end
--[===[
This code was generated by ChatGPT4o
Explanation of the code components:
Vector Operations with Metatables:
Defines a Vector class using metatables for vector addition, subtraction, and string representation.
The vectors are used and printed to demonstrate their operations.
Closure Example (Counter):
A create_counter function that creates a closure, allowing state to persist across function calls.
Coroutine Example (Fibonacci Generator):
An implementation of merge sort, demonstrating recursion, table manipulation, and table sorting.
This code combines many Lua features and should thoroughly test your Lua parser! Let me know if you'd like any modifications or additional features added.
A Fibonacci number generator using coroutines, yielding the next number in the sequence on each resume.
Recursive Function (Merge Sort):
]===]