-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.lua
130 lines (112 loc) · 2.57 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
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
--chapter 13
function div0(a, b)
if b == 0 then
error("DIV BY ZERO !")
else
return a / b
end
end
function div1(a, b) return div0(a, b) end
function div2(a, b) return div1(a, b) end
ok, result = pcall(div2, 4, 2); print(ok, result)
ok, err = pcall(div2, 5, 0); print(ok, err)
ok, err = pcall(div2, {}, {}); print(ok, err)
-- chapter 12
-- t={a=1,b=2,c=3}
-- for k,v in pairs(t) do
-- print(k,v)
-- end
-- t={"a","b","c"}
-- for k,v in ipairs(t) do
-- print(k,v)
-- end
-- chapter 11
-- local mt = {}
-- function vector(x, y)
-- local v = {x = x, y = y}
-- setmetatable(v, mt)
-- return v
-- end
-- mt.__add = function(v1, v2)
-- return vector(v1.x + v2.x, v1.y + v2.y)
-- end
-- mt.__sub = function(v1, v2)
-- return vector(v1.x - v2.x, v1.y - v2.y)
-- end
-- mt.__mul = function(v1, n)
-- return vector(v1.x * n, v1.y * n)
-- end
-- mt.__div = function(v1, n)
-- return vector(v1.x / n, v1.y / n)
-- end
-- mt.__len = function(v)
-- return (v.x * v.x + v.y * v.y) ^ 0.5
-- end
-- mt.__eq = function(v1, v2)
-- return v1.x == v2.x and v1.y == v2.y
-- end
-- mt.__index = function(v, k)
-- if k == "print" then
-- return function()
-- print("[" .. v.x .. ", " .. v.y .. "]")
-- end
-- end
-- end
-- mt.__call = function(v)
-- print("[" .. v.x .. ", " .. v.y .. "]")
-- end
-- v1 = vector(1, 2); v1:print()
-- v2 = vector(3, 4); v2:print()
-- v3 = v1 * 2; v3:print()
-- v4 = v1 + v3; v4:print()
-- print(#v2)
-- print(v1 == v2)
-- print(v2 == vector(3, 4))
-- v4()
-- chapter 10
-- function newCounter( )
-- local count =0
-- return function ()
-- count=count+1
-- return count
-- end
-- end
-- c1=newCounter()
-- print(c1())
-- print(c1())
-- c2=newCounter()
-- print(c2())
-- print(c1())
-- print(c2())
-- chapter 9
-- print(123,{2,5,89},"Hello World")
-- chapter 8
-- local function max( ... )
-- local args={...}
-- local val,idx
-- for i=1,#args do
-- if val==nil or args[i]>val then
-- val,idx =args[i],i
-- end
-- end
-- return val,idx
-- end
-- local function assert( v )
-- if not v then fail() end
-- end
-- local v1=max(3,9,7,128,35)
-- assert(v1==128)
-- local v2,i2=max(3,9,7,128,35)
-- assert(v2==128 and i2==4)
-- local v3,i3=max(max(3,9,7,128,35))
-- assert(v3==128 and i3==1)
-- local t={max(3,9,7,128,35)}
-- assert(t[1]==128 and t[2]==4)
-- chapter 7
-- local t={"a","b","c"}
-- t[2]="8"
-- t["foo"]="Bar"
-- local s=t[3]..t[2]..t[1]..t["foo"]..#t
-- local t={"a","b","c"}
-- t[2]="8"
-- local s=t[3]..t[2]..t[1]..t["foo"]..#t