我正在用Lua切割牙齿并试图实现一些列表处理逻辑。我不确定我是否真正掌握了协同程序和匿名函数的功能,但我正在努力。我知道这些东西在Ruby / Groovy / Javascript等其他脚本语言中是如何工作的,我想在Lua中做一些同样聪明的事情。以下是我提出的例子:
model = { { player = "Cliff", age = 35, gender = "male" }, { player = "Ally", age = 36, gender = "female" }, { player = "Jasmine", age = 13, gender = "female" }, { player = "Lauren", age = 6.5, gender = "female" } }
function allplayers()
return coroutine.create(function()
for idx, each in ipairs(model) do
coroutine.yield(idx, each)
end
end)
end
function handlePlayers(source)
local status, idx, each = coroutine.resume(source)
while each do
print(idx, each.player)
status, idx, each = coroutine.resume(source)
end
end
function having(source, predicate)
return coroutine.create(function()
local status, idx, each = coroutine.resume(source)
while each do
if predicate(each) then
coroutine.yield(idx, each)
end
status, idx, each = coroutine.resume(source)
end
end)
end
handlePlayers(having(allplayers(), function(each) return each.age < 30 end))
理想情况下,我希望能够编写如下代码:
allplayers(having(function(each) return each.age < 30 end))
甚至更好:
allplayers(having({each.age < 30 }))
产生相同的输出,但我无法理解如何或甚至可以做到这一点。对于所有迭代和循环以及所有迭代和循环,我上面所看到的东西似乎过于冗余。有没有更古老的方法来做到这一点? (男孩,我想念Groovy中的编码,因为总有一种更加时髦的方式来做某事......)
答案 0 :(得分:4)
如果您不需要重用过滤结果,请考虑使用迭代器:
model = {
{ player = "Cliff", age = 35, gender = "male" },
{ player = "Ally", age = 36, gender = "female" },
{ player = "Jasmine", age = 13, gender = "female" },
{ player = "Lauren", age = 6.5, gender = "female" }
}
function model:having(predicate)
local index = 0
return function()
while true do
index = index + 1
if index > #self then break end
if predicate(self[index]) then return self[index] end
end
end
end
for item in model:having(function(m) return m.age < 30 end) do
print(item.player)
end
答案 1 :(得分:1)
您可能希望在Underscore.lua JavaScript库之后模拟Underscore.js。
您的示例如下所示:
_ = require 'underscore'
model = {
{ player = "Cliff", age = 35, gender = "male" },
{ player = "Ally", age = 36, gender = "female" },
{ player = "Jasmine", age = 13, gender = "female" },
{ player = "Lauren", age = 6.5, gender = "female" }
}
result1 = _.select(model, function(p) return p.age < 30 end) -- traditional way
result2 = _(model):select(function(p) return p.age < 30 end) -- object-oriented style, calls can be chained
assert(_.is_equal(result1, result2))
请注意,Lua不会为您提供任何其他语法糖(除了:
运算符)。如果你真的想要更短的语法,你应该看看Metalua,它允许你改变语言,并提供开箱即用的以下短函数语法:
result = _.select(model, |p| p.age < 30)
答案 2 :(得分:0)
这令人难以置信的过于复杂和过度设计。您想通过谓词过滤一个列表。那样做;这是一个简单的循环。写下来吧。
即使你绝对必须以功能方式执行此操作(Lua不是一种功能语言。它可以这样工作,但它不起作用),协同程序不合适。
观察:
model = {
{ player = "Cliff", age = 35, gender = "male" },
{ player = "Ally", age = 36, gender = "female" },
{ player = "Jasmine", age = 13, gender = "female" },
{ player = "Lauren", age = 6.5, gender = "female" }
}
function for_each_array(list, operation)
for key, value in ipairs(list) do
operation(value)
end
end
function filter_if(list, predicate)
return function(value)
if(predicate(value)) then
list[#list + 1] = value
end
end
end
local list = {}
for_each_array(model, filter_if(list, function(each) return each.age < 30 end))
for_each_array(list, function(each) print(each.player) end)
请参阅?根本不需要协同程序。