Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Lua Learning Notes III (High-Level Topics)


May 12, 2021 Lua


Table of contents


High-level topics

1. Iteration

1.1 Instance code:

    --迭代
    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)

Output:

1

2

3

1.2 Description of the iteration:

  • Defined

Iteration is a special form of for statements that can be used to drive iterative functions to traverse a given collection. Formal, complete syntax instructions are complex, please refer to the Lua manual.

  • Realize

As the previous code shows: the enum function returns an anonymous iterative function, and each time the for statement calls the iterative function, it gets a value (referenced by the element variable), which, if nil, ends the for loop.

2. Collaboration thread

2.1 instance code

    --线程
    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())

Output:

11

102

10003

END!

2.2 Description of the collaboration thread:

  • Create a collaboration thread

With coroutine.create, you can create a collaboration thread that receives arguments of a function type as the executor of the thread and returns a thread object.

  • Start the thread

You can start a thread or continue a suspended thread with coroutine.resume. T he function receives a thread object and other parameters that need to be passed to the thread. T hreads can get these parameters by the parameters of the thread function or by the return value of the coroutine.yield call. W hen the thread executes for the first time, the parameters passed by resume are passed to the thread through the parameters of the thread function, which starts with the thread function; When a thread changes from a pending to execution, the argument passed by resume is passed to the thread in the form of a return value of the yield call, which continues after the yield call

  • The thread abandons the schedule

    The thread calls coroutine.yield to suspend its execution and returns the execution to the thread that started/continued it; T hreads can also take advantage of yield to return values to the latter, which are returned as return values called by resume.

Appendix CommonLy Used Lua References

lua Forum (lua China developer luaer China lawsuit website)

Lua Reference Manual (most formal and authoritative Lua document)

Lua Programming (online version, equally authoritative Lua textbook)

Document page on Lua's official website (contains links to many valuable documents)

Lua Wiki (the most comprehensive Lua Wikipedia)

LuaForge (the richest Lua open source base)

Reference " Luke's Quick Start for C/ C ? Programmers"