Lua的迭代器转换为数组

在 Lua 术语中,是否有将迭代器函数转换为数组(通过重复调用并将结果存储在升序索引中)的语法糖,或者标准库中有什么方法吗?

我正在对属于协议的字符串进行令牌化,并且需要在该字符串开头的位置上访问元素,并且该字符串的末尾是一个变体集合。

以下是特定于我的用例的代码,我很难相信它不在标准库中:d

local array_tokenise = function (line)
    local i = 1;
    local array = {};

    for item in string.gmatch(line,"%w+") do
      array[i] = item;
      i = i +1
    end

    return array
  end

原文链接 https://stackoverflow.com/questions/8316251

点赞
stackoverflow用户734069
stackoverflow用户734069

没有标准的库函数来处理它。但实际上,编写它非常简单:

function BuildArray(...)
  local arr = {}
  for v in ... do
    arr[#arr + 1] = v
  end
  return arr
end

local myArr = BuildArray(<iterator function call>)

这个函数只能处理迭代器函数返回一个单独元素的情况。如果它返回多个元素,你需要采用其他方法。

2011-11-29 19:13:59
stackoverflow用户837856
stackoverflow用户837856

正如Nicol Bolas所说,没有标准库函数可以执行您所需的操作。

下面是一个扩展table库的实用函数:

function table.build(build_fn, iterator_fn, state, ...)
    build_fn = (
            build_fn
        or  function(arg)
                return arg
            end
    )
    local res, res_i = {}, 1
    local vars = {...}
    while true do
        vars = {iterator_fn(state, vars[1])}
        if vars[1] == nil then break end
        --build_fn(unpack(vars)) -- see https://web.archive.org/web/20120708033619/http://trac.caspring.org/wiki/LuaPerformance : TEST 3
        res[res_i] = build_fn(vars)
        res_i = res_i+1
    end
    return res
end

这里是一些示例代码,演示如何使用:

require"stringify"

local t1 = {4, 5, 6, {"crazy cake!"}}
local t2 = {a = "x", b = "y", c = "z"}
print(stringify(table.build(nil, pairs(t1))))
print(stringify(table.build(nil, pairs(t2))))
print(stringify(table.build(
        function(arg) -- arg[1] = k, arg[2] = v
            return tostring(arg[1]).." = "..tostring(arg[2])
        end
    ,   pairs(t1)
)))

local poetry = [[
    Roses are red, violets are blue.
    I like trains, and so do you!

    By the way, oranges are orange.
    Also! Geez, I almost forgot...
    Lemons are yellow.
]]
print(stringify(table.build(
        function(arg) -- arg[1] == plant, arg[2] == colour
            return (
                    string.upper(string.sub(arg[1], 1, 1))..string.lower(string.sub(arg[1], 2))
                ..  " is "
                ..  string.upper(arg[2]).."!"
            )
        end
    ,   string.gmatch(poetry, "(%a+)s are (%a+)")
)))

输出:

{
    [1] = {
        [1] = 1,
        [2] = 4,
    },
    [2] = {
        [1] = 2,
        [2] = 5,
    },
    [3] = {
        [1] = 3,
        [2] = 6,
    },
    [4] = {
        [1] = 4,
        [2] = {
            [1] = "crazy cake!",
        },
    },
}
{
    [1] = {
        [1] = "a",
        [2] = "x",
    },
    [2] = {
        [1] = "c",
        [2] = "z",
    },
    [3] = {
        [1] = "b",
        [2] = "y",
    },
}
{
    [1] = "1 = 4",
    [2] = "2 = 5",
    [3] = "3 = 6",
    [4] = "4 = table: 00450BE8",
}
{
    [1] = "Rose is RED!",
    [2] = "Violet is BLUE!",
    [3] = "Orange is ORANGE!",
    [4] = "Lemon is YELLOW!",
}

可以在这里找到stringify.lua

2011-11-30 11:35:57
stackoverflow用户508413
stackoverflow用户508413

如果你只是想为每个数据元素自动递增表键,你可以使用 table.insert(collection, item) - 这将把 item 添加到 collection 并将键设置为 collection 的数量加 1。

2011-11-30 14:47:43