lua programming (a)

Abstract: lua programming second edition of the study notes

  Most basic grammar is relatively simple scripting language, just to name some of the unique lua, or need to pay special attention to the grammar.

  Contents of the book are some of the usual three chapters introduction, the basic data types, operators, etc., is relatively simple, not repeat them here.

 

Statement

  1, do ... end can be used to contain a block.

  2, local variables declared in the loop, when the determination condition persists

  3, for the cycle is divided into numeric and generics

    Digital type:

. 1    for var = EXP1, EXP2, EXP3 do 
2          <executable>
 . 3    End

  It will be similar to the brackets and period C is removed, var initial value exp1, to increase exp2, the step of exp3.

  exp3 optional, defaults to +1, no upper limit may be set exp2 math.huge.

  for control variables are automatically declared as local variables for the no separate statement, but can not access externally.

 

    Generic for: access the table through the iterator

. 1    for I, V in  ipairs (A) do  Print (V) End     - print all values of 
2    for K in  pairs (T) do  Print (K) End        - print all key

  4, lua in a variety of iterators, it may be self-prepared

    Iteration file line io.lines

    Iteration table element pairs

    Iterative array elements ipairs

    Iteration string of words string.gmatch

  5, return, break statement only as a last code block, or end, else, until before the last statement, or lua syntax error, by explicitly do return end wrap a return.

 

function

6, lua a function, if only one parameter, and this parameter is a literal string or a table-type configuration, function () can be omitted.

7, lua special object-oriented syntax is - colon operator, the value itself as the first pass.

8, lua in functions can return multiple values, while receiving multiple variables, multiple assignment is similar to, but if the value is not a function call last of a series of expressions, only to produce a value .

9, in a phenomenally, it is also effective in a multi-function return value as a non-last argument another function.

10, the function call placed by a pair of parentheses may be the force which returns a value.

11, unpack () function accepts a parameter array, and returns all the parameters one by one from the subscript 1, used in a generic call.

12, a function is declared, the parameters (...), that is variable-length argument in the function '...' is used as an expression.

13, variable length parameters and fixed parameters simultaneously function parameters, the parameters to be placed in the last variable length.

14, when the variable-length argument passed intentionally contained nil, need select access function, the function must first select a fixed argument passing, if the argument is a number n, then the function returns the n-th variable length parameter ( including nil), or variable-length argument must be a '#', returns the total number of variable length.

15, named function, similar to the python specified function parameter assignment, but need to be written to the parameter name and value in a table, passed to the function.

16, lua function is a first class values, in fact, the function name in lua understood as a variable to hold a function more appropriate.

1 function foo (x)  return 2*x end    等价于
2 foo = function (x)  return 2*x  end

   This makes it possible easily lua correction mode, e.g. ranking function provided in C ++ STL, bool need to pass a return value of the function pointer type, for comparing the size of the variable container, lua can be achieved,

   For example, a function to sort the table:

1 network = {............}
2 table.sort(network, function (a, b) return (a.id > b.id) end)

   For a high-end applications of this feature, the derivative:

 1  function derivative(f, delta)
 2      delta = delta or 1e-4
 3      return function(x)
 4                return (f(x + delta) - f(x))/delta
 5       end
 6  end       
 7 
 8  c = derivate(math.sin)
 9  print(math.cos(10), c(10))
10     -->  -0.83907152907645  -0.83904432662041

 

Depth function

  17, closure (closure function), from the translation do not really understand the concept. First, when a function defined inside another function, then the function of the inner layer of the outer function can access variables, this feature is called the " lexical domain ." And this variable relative to an internal function called

As " non-local variables for all non-local variable", a closure is a function plus the function required access.

 1  function newCount()
 2  local i = 0
 3     return function() i = i + 1 return i end
 4  end
 5 
 6  c1 = newCount()
 7  print(c1())      --> 1
 8  print(c1())      --> 2
 9  c2 = newCount()
10  print(c2())     --> 1
11  print(c1())     --> 3
12  print(c2())     --> 2

  c1 and c2 are two different closure created by the same function, they have their own instance of the local variable i.

  18, closure Another use is to create a secure sandbox environment to run some untrusted code.

 1 do
 2 local oldOpen = oi.open
 3 local access_OK = function(filename, mode)
 4     <检查访问权限>
 5 end
 6 io.open = function(filename, mode)
 7     if access_OK(filename, mode) then
 8         return oldOpen(filename, mode)
 9     else
10        return nil, "access denied"
11    end
12 end       
13 end  

  19, non-global functions, such as members of a table or a local modification of the function declaration only in the current block access to this function. One thing to note, when the local recursive function definition, when a recursive function because the local has not been defined, it is in fact calling

The same name as a global function, as the function name to solve this problem by first define a local variable.

  20, for non global function, there is a syntactic sugar Lua:

. 1  local  function foo (<parameter>) <function body> End

  lua to expand it to:

. 1  local foo
 2 foo = function (<parameter>) <function body> End

  This definition does not produce recursive error. Of course, for this is an invalid indirect recursion, indirect recursion must be a forward declaration, the sample code is not posted, indirect recursion is really kind of bad grammar, do not use unless necessary.

  21, tail call, the correct call of the form Lua tail follows:

1 return <function>(<args>)

  Goto statement is equivalent to a tail call, because the code has no need to perform the calling function, the returned value may be directly called function, that is, the return value of the function to cover the end of the call, it does not occur at this time recursion stack overflow problems, while this feature can also be used in lua

The state machine implementation, with tail call to jump to the next state, without any memory problems.

 

Guess you like

Origin www.cnblogs.com/Dylan7/p/11817225.html