IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

二十五、Lua 学习笔记之三(高阶话题)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=8620

高阶话题

1.迭代

1、1 实例代码:

    --迭代
    local function enum(array)
        local index = 1
        return function()
            local ret = array[index]
            index = index + 1
            return ret
        end
    end

    local function foreach(array,action)
        for element in enum(array)do
            action(element)
        end
    end

    foreach({1,2,3},print)

输出结果:

1

2

3

1、2 有关迭代的描述:

  • 定义

迭代是for语句的一种特殊形式,可以通过for语句驱动迭代函数对一个给定集合进行遍历。正式、完备的语法说明较复杂,请参考Lua手册。

  • 实现

如前面代码所示:enum函数返回一个匿名的迭代函数,for语句每次调用该迭代函数都得到一个值(通过element变量引用),若该值为nil,则for循环结束。

2.协作线程

2、1 实例代码

    --线程
    local function producer()
        return coroutine.create(
        function(salt)
            local t = {1,2,3}
            for i = 1,#t do
                salt = coroutine.yield(t[i] + salt)
            end
        end
        )
    end

    function consumer(prod)
        local salt = 10
        while true do
            local running ,product = coroutine.resume(prod, salt)
            salt = salt*salt
            if running then
                print(product or "END!")
            else
                break
            end
        end
    end

    consumer(producer())

输出结果:

11

102

10003

END!

2、2 有关协作线程的描述:

  • 创建协作线程

通过coroutine.create可以创建一个协作线程,该函数接收一个函数类型的参数作为线程的执行体,返回一个线程对象。

  • 启动线程

通过coroutine.resume可以启动一个线程或者继续一个挂起的线程。该函数接收一个线程对象以及其他需要传递给该线程的参数。线程可以通过线程函数的参数或者coroutine.yield调用的返回值来获取这些参数。当线程初次执行时,resume传递的参数通过线程函数的参数传递给线程,线程从线程函数开始执行;当线程由挂起转为执行时,resume传递的参数以yield调用返回值的形式传递给线程,线程从yield调用后继续执行

  • 线程放弃调度

    线程调用coroutine.yield暂停自己的执行,并把执行权返回给启动/继续它的线程;线程还可利用yield返回一些值给后者,这些值以resume调用的返回值的形式返回。

附录 常用的Lua参考资料

lua 论坛(lua 中国开发者 luaer中国官司方网站)

Lua参考手册(最正式、权威的Lua文档)

Lua编程(在线版,同样具权威性的Lua教科书)

Lua正式网站的文档页面(包含很多有价值的文档资料链接)

Lua维基(最全面的Lua维基百科)

LuaForge(最丰富的Lua开源代码基地)

参考文献《C/C++程序员的Lua快速入门》

干货推荐


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(70) 打赏



未经允许不得转载:搜云库技术团队 » 二十五、Lua 学习笔记之三(高阶话题)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367