Lua学习笔记函数
来源:互联网 发布:淘宝网床上用品 编辑:程序博客网 时间:2024/05/18 09:26
数字型for 的格式,exp3默认为1
for var=exp1 , exp2 , exp3 do
<执行体>
end
泛型for迭代的类型,标准库中提供了几种迭代器:
io.lines() 用于迭代文件中每行
pairs() 迭代table中元素
ipairs() 迭代 数组 元素
string.gmatch() 迭代字符串中单词
一,函数
多重返回值:x , y = foo()
foo()返回值就是2个值,并不是table或者其他类型。
t = { foo() } 这是再封装 成为一个table。{ “a” , “b” }
变长参数:function add ( ... )
在函数内部使用这些参数 依然使用 ... 来表示,此时,这个表达式 是一个像多重返回值一样的序列,不是table。当然,可以再包装 { ... }。这也是通常函数内遍历变长参数的方式。
function fwrite ( fmt , ... )
return io.write( string.format( fmt, ...) )
end
具名实参:
w = Window{ x=0 , y=0 , width=300 , height=200,
title = “Lua” , background=”blue”,
border = true }
Window函数只有一个参数(当函数只有一个参数时,调用时可以去掉圆括号),并且,这个参数需要我们用table,就可以实现具名参数了。
深入函数:
第一类值:函数(这个函数本身,而非其返回值,其实现我们可以理解为类似于C中的函数指针被存储)可以存储到变量中,或者table中,可以作为实参传递给其他函数,还可以作为作为其他函数的返回值。词法域:类似于JS中的闭包。
函数名,一个拥有某函数的变量。函数与所有其他值一样都是匿名的。
foo = function( x ) return 2*x end
table.sort ( onetable , function( a , b )
return (a.name > b.name)
end
)
对一些函数的访问,想要建立一个安全的运行环境,比如,限制一个程序访问文件:
do
local oldOpen = io.open
local access_OK = function ( filename , mode )
<检查访问权限>
end
io.open = function ( filename , mode )
if access_OK( filename , mode ) then
return oldOpen( filename , mode )
else
return nil , “access denied”
end
end
end
正确的尾调用:
function f ( x ) return g( x ) end ---yes
function f ( x ) g( x ) end ---not
“尾调用”不需要消耗任何栈信息,即,当g返回时,程序执行点将直接返回 调用f的地方。可以理解为,在f返回并调用g的时候,f 已经被清栈,之后接着调用g,g结束后自然栈调用信息直接到了,f的调用者的地方。
“尾调用” return <func> ( <args> )
非常适合状态机一类程序。
截取一段例子:
funciton room1 ()
local move = io.read()
if move == “south” then return room3()
elseif move == “east” then return room2()
else
print( “invalid move” )
return room1() --stay in the same room
end
end
- Lua学习笔记函数
- Lua string函数学习笔记
- Lua学习笔记3:函数
- lua学习笔记之函数
- 5.lua学习笔记:函数
- lua学习笔记---Function(函数)
- Lua学习笔记--函数初探
- Lua学习笔记(函数)
- lua学习笔记二--函数
- Lua学习笔记 第五章 函数
- Lua学习笔记 第六章 深入函数
- lua学习笔记之浅淡unpack()函数
- Lua中文教程学习笔记 (4) 函数
- Lua学习笔记(2)-语句、函数
- Lua学习笔记9-函数知识总结
- lua脚本语言学习笔记(二)---LUA函数\数组\迭代器
- [LUA学习笔记03]LUA函数和C函数的相互调用
- Lua学习笔记(二)—— 函数
- 现在公开一个DHT网络爬虫
- APOC-UVA-0003 题解
- CSS基础
- OCP 1Z0 053 91
- oracle sql计划基线
- Lua学习笔记函数
- 【LeetCode with Python】 Edit Distance
- android之VideoView和视频播放View的扩展
- 《老罗Android第二季》ViewPager分页及JSON
- TNS-01106: Message 1106 not found; No message file for product=network, facility=TNS(转)
- map、hashmap
- 闲暇时间看了下DHT网络
- Android Adapter学习之 用法总结
- javascript总结