Basics Lua learning Part Ten <Lua common grammatical rules>

The following talk about some lua common usage and rules, can help bring learning to understand lua.

1. if Analyzing

lua to nil and false as "false", the other "true"

2. The logical operators and or

The lua and or a triplet of expressions may be used to form, as follows:

> = 1 == 1 and 1 or 2
1

But if this is written, it is not the result you want:

> = 1 == 1 and false or true
true

This is because, and operator false judgment is not satisfied, it continued to implement the results or operator (nil and false lua believe is false)
If you must return true or false, you wrote:

> = ( 1 == 1 and {false} or {true} )[1]
false

3. local variable declaration

local var1 = 1, var2
above, different var1 var2 and scope, where the variables var1 scope, var2 may be a global variable. After the explanation of the above command is actually taking var1 "1, var2" consisting of a first value, similar to the local var1 = ...
Please note that the variable declaration, if in doubt, honestly Yiyi assignment.

local var1
local var2=1
print(var1)  --nil
print(var2)  --1
print("=====")
local var3 =1,var4
print(var3) --1
print(var4) --nil
print("=====")
local  var5, var6=1
print(var5) --1    ->是不是很奇怪-  -、这里的var5竟然是1
print(var6) --nil

4. table is empty if

if a == {} then

The result is false, this is a logical error, is the same as the actual memory address A comparison table and a table of anonymous

The correct wording is:

if next(a) == nil then

5. Multiple variable assignments

name,name = 1,2

How much of it is equal to that name?

Indeed name value 1, a small example can write a, b = 2,3,4 can be seen on the print. a = 2, b = 3

6. table of key rules

t[name]与t["name"], t.name

The first and the latter two are different, a first key will be taken according to the value of the name, the latter two to "name" do key. This case also:
T = {[name]}. 1 =

t = {name = 1}
t = {["name"] = 1}

7. table of length

The length of the table to obtain the most common practice is #table, as follows:

> t = {1,2,3}
> #t
3

But the # operator is also limited, computational objects other than the array is not supported, so to remember

> t = { 10, n = 20, 30, 40 }
> #t
3 --长度是4,所以,要采用其他方式判定长度

In addition, by setting the nil elements can not change the result of #table (unless it is the last element of the array); and table.remove you can update the results immediately #table

t ={1,2,3}
t[1]=nil
print(#t) --3

table.remove(t,1)
print(#t) --2

8. table reference issues

Will copy a table to another table, modify the new table will affect the value of the original table, but you can change this behavior by clone

function deepcopy(orig)
    local orig_type = type(orig)
    local copy
    if orig_type == 'table' then
        copy = {}
        for orig_key, orig_value in next, orig, nil do
            copy[deepcopy(orig_key)] = deepcopy(orig_value)
        end
        setmetatable(copy, deepcopy(getmetatable(orig)))
    else -- number, string, boolean, etc
        copy = orig
    end
    return copy
end

9. The function returns the value

function f123() 
    return 1, 2, 3
 end
function f456()
     return 4, 5, 6
 end
 print(f123())  --1 2 3
 print(f456())  --4 5 6

 print(f123(), f456()) -- 1 4 5 6
 print(f456(),1) --4 1

If the function is not in the last column, return only one value

10. The floating point problem

This is a float will be some loss of accuracy problems, lua also have this problem. Comparison of integer and floating-point look at

> =10==10.00000000000000000
true
> =10==10.00000000000000001
true
> =10==10.00000000000000000
true
> =10==9.999999999999999999
true

So, here, test it, lua floats will be accurate to 15, it was observed that good accuracy before making comparisons more accurate comparison

11. colon syntax

The colon syntax is used to define the function, in this case, there is an implicit function of the parameter self.

Worded as follows: function T: F (the params) End body colon syntax is actually a syntactic sugar, is equivalent to: TF = function (Self, the params) End body


local t={a=123}

function t.f1(self, p)
    print(self.a, p) --self.a 访问t中a的值
end

function t:f2(p)
    print(self.a, p)
end

t:f1(1)
t.f1(t,1)
t:f2(1)
t.f2(t,1)

The results are more than a few 1231
Here, the function call by way of the colon without the addition of self argument, while the dot is required.

12. The closure function

When a function is nested inside another function definition, function body can access the local variables inside a function of external, we call such a feature word scoping.

function fn()
    local i = 0
    return function()     -- 注意这里是返回函数的地址,不是执行
       i = i + 1
        return i
    end
end
 
c1 = fn()           -- 接收函数返回的地址
print(c1())  --> 1          --c1()才表示执行 ,i 此时为0
print(c1())  --> 2          --继续执行函数体 此时i =1上次执行的结果

As above, when calling c1 (), fn clear function has returned, Lua Closures Closures thought correctly handle this situation:

  • We call i is a local variable from outside of c1 (external local variable) or upvalue.
  • Simply put, the closure is a function and its upvalues

If we call fn again, will create a new local variable i:

c2 = fn()
print(c2())  --> 1
print(c1())  --> 3
print(c2())  --> 2

13. End function calls

  • Similar tail call is a function call goto end.
  • When was the last action function calls another function, we call the end of this call is made.
function f(x)
    return g(x)  -- 类似于goto g(x)函数的地址
end
  • Last call does not require the use of stack space, so the tail call recursion level can be unlimited.

For example, the following call no matter what the value of n does not cause a stack overflow.

function foo (n)
    if n > 0 then return foo(n - 1) end
end

Note that: You must be clear what is the tail call.
Some callers after the function call other functions did not do other things, but not a tail call. such as:

function f (x)
    g(x)
    return
end

In the above example the call g f, g return value to be discarded, so it is not tail call, the following examples are not the same when the tail call:

return g(x) + 1      -- 还需+1
return x or g(x)     -- 还需比较
return (g(x))        -- 还需调整为一个返回值

Guess you like

Origin www.cnblogs.com/xiaoqiangink/p/12083006.html