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
|
print("=== Lua Module Usage Example ===")
print("\n1. Importing Module") local math_module = require("mymodule")
print("\n2. Accessing Module Constants") print("PI:", math_module.PI) print("E:", math_module.E) print("GRAVITY:", math_module.GRAVITY)
print("\n3. Calling Module Functions")
print("\n3.1 Geometric Calculations") local circle_radius = 5 local circle_area = math_module.circle_area(circle_radius) print("Circle area with radius " .. circle_radius .. ":", circle_area)
local rect_width, rect_height = 4, 6 local rect_area = math_module.rectangle_area(rect_width, rect_height) print("Rectangle area (" .. rect_width .. "x" .. rect_height .. "):", rect_area)
local tri_base, tri_height = 3, 7 local tri_area = math_module.triangle_area(tri_base, tri_height) print("Triangle area (base=" .. tri_base .. ", height=" .. tri_height .. "):", tri_area)
print("\n3.2 Mathematical Functions") local fact_num = 5 local fact_result = math_module.factorial(fact_num) print("Factorial of " .. fact_num .. ":", fact_result)
print("\n3.3 Prime Number Check") local test_nums = {2, 3, 4, 17, 20, 23, 100} for _, num in ipairs(test_nums) do local is_prime = math_module.is_prime(num) print(num .. " is prime?", is_prime) end
print("\n3.4 Fibonacci Sequence") local fib_count = 10 local fib_sequence = math_module.fibonacci(fib_count) print("First " .. fib_count .. " Fibonacci numbers:", table.concat(fib_sequence, ", "))
print("\n3.5 Greatest Common Divisor and Least Common Multiple") local a, b = 24, 36 local gcd_result = math_module.gcd(a, b) local lcm_result = math_module.lcm(a, b) print("GCD of " .. a .. " and " .. b .. ":", gcd_result) print("LCM of " .. a .. " and " .. b .. ":", lcm_result)
print("\n3.6 Degree and Radian Conversion") local degrees = 90 local radians = math_module.deg_to_rad(degrees) print(degrees .. " degrees = ", radians, " radians")
local radians_val = math_module.PI / 2 local degrees_val = math_module.rad_to_deg(radians_val) print(radians_val .. " radians = ", degrees_val, " degrees")
print("\n4. Different Module Import Methods")
print("\n4.2 Selective Function Import") local is_prime, factorial = math_module.is_prime, math_module.factorial print("Using selectively imported functions:") print("is_prime(17):", is_prime(17)) print("factorial(6):", factorial(6))
print("\n4.3 Renaming Imported Module") local mm = require("mymodule") print("Using renamed module (mm):") print("mm.PI:", mm.PI) print("mm.circle_area(3):", mm.circle_area(3))
print("\n5. Error Handling in Module Functions")
local success, result = pcall(math_module.circle_area, -5) if not success then print("Error caught:", result) end
local test_cases = { {func = math_module.rectangle_area, args = {"invalid", 5}, desc = "Invalid width (string)"}, {func = math_module.triangle_area, args = {3, -2}, desc = "Negative height"}, {func = math_module.factorial, args = {-1}, desc = "Negative factorial"} }
for _, case in ipairs(test_cases) do local success, err = pcall(case.func, unpack(case.args)) if not success then print(case.desc .. " error:", err) end end
print("\n6. Multiple Imports of the Same Module")
local module1 = require("mymodule") local module2 = require("mymodule")
print("Are module1 and module2 the same instance?", module1 == module2)
print("\n7. Module Extension")
function math_module.quadratic_area(side) return side * side end
local square_side = 4 local square_area = math_module.quadratic_area(square_side) print("Square area with side " .. square_side .. ":", square_area)
print("\n=== End of Module Usage Example ===")
|