An analysis of iterators in Lua

Source: Internet
Author: User
Tags closure data structures lua

This article mainly introduces the iterator in Lua, is the basic knowledge in the LUA initiation study, the need friend can refer to the following

An iterator is a structure that enables you to traverse an element in a so-called collection or container. In Lua, these collections usually refer to those that are used to create various data structures, such as arrays.

Generally for iterators

A generic iterator provides key value pairs for each element in the collection. The following is a simple example.

The code is as follows:

Array = {"Lua", "Tutorial"}

For Key,value in Ipairs (array)

Todo

Print (key, value)

End

When we run the above code, we get the following output

The code is as follows:

1 Lua

2 Tutorial

The above example uses the default Ipairs iteration function provided by LUA.

In Lua, we use functions to represent iterators. There are two main types of functional state retention based on these iterators:

Non-state iterator

Iterator state

Non-state iterator

The name itself makes it clear that this type of iterator feature does not retain any state.

Now let's take a look at the example of creating our own iterators using the simple square function of printing n numbers.

The code is as follows:

function Square (Iteratormaxcount,currentnumber)

If Currentnumber

Then

Currentnumber = currentnumber+1

Return Currentnumber, Currentnumber*currentnumber

End

End

For I,n in square,3,0

Todo

Print (I,n)

End

When we run the above program, we get the output below.

The code is as follows:

1 1

2 4

3 9

The code above can be modified slightly to mimic the way iterators work ipairs functions. It looks like the following.

The code is as follows:

function Square (Iteratormaxcount,currentnumber)

If Currentnumber

Then

Currentnumber = currentnumber+1

Return Currentnumber, Currentnumber*currentnumber

End

End

function squares (iteratormaxcount)

Return square,iteratormaxcount,0

End

For I,n in Squares (3)

Todo

Print (I,n)

End

When we run the above program, we get the output below.

The code is as follows:

1 1

2 4

3 9

Iterator state

The iterative use feature does not preserve the state, the preceding example. Each time the function is called, it returns the next element based on the collection of the second variable sent to the function. To preserve the state of the current element, using the enclosing object. Closes the value that retains the call variable of different functions. To create a new closure, we created two functions, including closing ourselves, a factory, and creating closed functions.

Now let's take a look at the example of creating our own iterations where we will use closures.

The code is as follows:

Array = {"Lua", "Tutorial"}

function Elementiterator (collection)

Local index = 0

Local count = #collection

--The closure function is returned

return function ()

index = index + 1

If Index <= count

Then

--Return to the current element of the iterator

return Collection[index]

End

End

End

for element in Elementiterator (array)

Todo

Print (Element)

End

When we run the above program, we get the output below.

The code is as follows:

Lua

Tutorial

In the above example, we can see that the element iteration has another method in which the index and count of local external variables used are incremented by each function when it is invoked, returning the elements in each collection.

We can create an iterator for functions that use closures, as shown in the figure above, which can return multiple elements, each of which we collect through the loop time.

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.