diff options
Diffstat (limited to 'ltn012.wiki')
-rw-r--r-- | ltn012.wiki | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/ltn012.wiki b/ltn012.wiki index b924dd3..96b13ae 100644 --- a/ltn012.wiki +++ b/ltn012.wiki | |||
@@ -126,8 +126,9 @@ local function chain2(f1, f2) | |||
126 | end | 126 | end |
127 | 127 | ||
128 | function filter.chain(...) | 128 | function filter.chain(...) |
129 | local arg = {...} | ||
129 | local f = arg[1] | 130 | local f = arg[1] |
130 | for i = 2, table.getn(arg) do | 131 | for i = 2, #arg do |
131 | f = chain2(f, arg[i]) | 132 | f = chain2(f, arg[i]) |
132 | end | 133 | end |
133 | return f | 134 | return f |
@@ -235,9 +236,10 @@ end | |||
235 | 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. | 236 | 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. |
236 | {{{ | 237 | {{{ |
237 | function source.cat(...) | 238 | function source.cat(...) |
239 | local arg = {...} | ||
238 | local co = coroutine.create(function() | 240 | local co = coroutine.create(function() |
239 | local i = 1 | 241 | local i = 1 |
240 | while i <= table.getn(arg) do | 242 | while i <= #arg do |
241 | local chunk, err = arg[i]() | 243 | local chunk, err = arg[i]() |
242 | if chunk then coroutine.yield(chunk) | 244 | if chunk then coroutine.yield(chunk) |
243 | elseif err then return nil, err | 245 | elseif err then return nil, err |