fix use of arg in ltn documentation

This commit is contained in:
Pierre Chapuis 2013-01-23 19:03:46 +01:00
parent d548a78e55
commit eea1bc04d7
2 changed files with 7 additions and 5 deletions

View File

@ -126,8 +126,9 @@ local function chain2(f1, f2)
end
function filter.chain(...)
local arg = {...}
local f = arg[1]
for i = 2, table.getn(arg) do
for i = 2, #arg do
f = chain2(f, arg[i])
end
return f
@ -235,9 +236,10 @@ end
We can make these ideas even more powerful if we use a new feature of Lua 5.0: coroutines. Coroutines suffer from a great lack of advertisement, and I am going to play my part here. Just like lexical scoping, coroutines taste odd at first, but once you get used with the concept, it can save your day. I have to admit that using coroutines to implement our file source would be overkill, so let's implement a concatenated source factory instead.
{{{
function source.cat(...)
local arg = {...}
local co = coroutine.create(function()
local i = 1
while i <= table.getn(arg) do
while i <= #arg do
local chunk, err = arg[i]()
if chunk then coroutine.yield(chunk)
elseif err then return nil, err

View File

@ -73,12 +73,12 @@ Fortunately, all these problems are very easy to solve and that's what we do in
We used the {{pcall}} function to shield the user from errors that could be raised by the underlying implementation. Instead of directly using {{pcall}} (and thus duplicating code) every time we prefer a factory that does the same job:
{{{
local function pack(ok, ...)
return ok, arg
return ok, {...}
end
function protect(f)
return function(...)
local ok, ret = pack(pcall(f, unpack(arg)))
local ok, ret = pack(pcall(f, ...))
if ok then return unpack(ret)
else return nil, ret[1] end
end
@ -157,7 +157,7 @@ function newtry(f)
if f then f() end
error(arg[2], 0)
else
return unpack(arg)
return ...
end
end
end