Lua Learning Notes functions, variable-length parameters, closure (closures), select, etc. _lua

Source: Internet
Author: User
Tags closure lua

1. The LUA function supports multiple return values, but not every value returned by the calling function is used.

One rule is that when a function call is the last element of an expression, its full return value is used. Look at the code:

Copy Code code as follows:

The--string.find function returns two values: the start index and end index of the lookup substring
S,e = String.find ("Lua Program language", "LUA")
Print (s,e)--> 1 3

--If not found, output nil and nil
S,e = String.find ("Lua Program Language", "lub")
Print (s,e)-->nil Nil


--Find the largest element in the array and its index
function Maximum (a)
Local mi = 1-The maximum element index, starting with the assumption that the first element is the largest element
Local m = A[mi]
For I,val in Ipairs (a) do
If M < Val Then
Mi,m = I,val
End
End
Return mi,m
End

Print (maximum{1,2,10,4,5,7})--> 3 10

--a function that returns multiple values can use multiple values that it returns only if it has the most element as an expression
--otherwise take only the first value


--Defines a function that returns two values
function foo () return "A", "B" end

--foo () at the end of the expression, using the two values he returned
A,b = foo ()-->a b
Print (A,B)

--foo () appears in the middle of an expression, using only the first value she returns
A,b,c = foo (), "C"
Print (A,B,C)-->a c Nil

--Using the two results returned by the function
Print (foo ())-->a b

--Put the function call in the middle bracket, forcing the function to return only one result
Print ((foo ()))--> A

2. Function support variable length parameter: ...
If there is no nil in the variable length parameter, then the Ipairs (...) can be used. To traverse to get all the arguments.

If there is a nil in the argument, then only the Select () function can be used. Because Ipairs can only traverse to the nil place.

Copy Code code as follows:

--Variable length parameter


--Complete the sum of the Nunmber list with variable length parameters
function Add (...)
Local sum = 0
For i,v in Ipairs (...) do
sum = sum + V
End
return sum
End

Print (add{1.1,2.2,3.3,4.4,nil,6,8})-->11 indicates that the 6,8 behind nil are not traversed to


--If the variable length parameter is intentionally passed into the nil
-Then use the Select function to access the variable-length argument list.
--select to the argument that if you pass in an integer n, it returns the list of the first element beginning to the end of the last element.
--Returns the total length of the argument list if "#" is passed in

function Add2 (...)
Local sum = 0
Local arg
For I=1,select (' # ',...) do

--visible from the output result, select (I,...) Returns the list of the first element beginning to the end of the last element
Print (select (i,...)) --&GT;2 4 6 Nil 5 8
-->4 6 Nil 5 8
--&GT;6 Nil 5 8
-->nil 5 8
-->5 8
-->8

--Take only the first value of the list
arg = select (I,...)
If Arg then
sum = sum + arg
End

End
return sum
End
Print (ADD2 (2,4,6,nil,5,8))-->25 indicates that the values behind nil are traversed to the

3.closure (closure)

My understanding is that internal functions can call local variables of external functions and can still use local variables of external functions after the end of an external function call. Each call to the external function will recreate a closure, and the previous one will not disappear. There is a question: When will closure be released?

Copy Code code as follows:

--Characteristics of closure

function Newcounter ()
Local i = 0
return function ()
i = i + 1
return I
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.