diff options
-rw-r--r-- | ltn012.wiki | 6 | ||||
-rw-r--r-- | ltn013.wiki | 6 |
2 files changed, 7 insertions, 5 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 |
diff --git a/ltn013.wiki b/ltn013.wiki index 734b433..a622424 100644 --- a/ltn013.wiki +++ b/ltn013.wiki | |||
@@ -73,12 +73,12 @@ Fortunately, all these problems are very easy to solve and that's what we do in | |||
73 | 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: | 73 | 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: |
74 | {{{ | 74 | {{{ |
75 | local function pack(ok, ...) | 75 | local function pack(ok, ...) |
76 | return ok, arg | 76 | return ok, {...} |
77 | end | 77 | end |
78 | 78 | ||
79 | function protect(f) | 79 | function protect(f) |
80 | return function(...) | 80 | return function(...) |
81 | local ok, ret = pack(pcall(f, unpack(arg))) | 81 | local ok, ret = pack(pcall(f, ...)) |
82 | if ok then return unpack(ret) | 82 | if ok then return unpack(ret) |
83 | else return nil, ret[1] end | 83 | else return nil, ret[1] end |
84 | end | 84 | end |
@@ -157,7 +157,7 @@ function newtry(f) | |||
157 | if f then f() end | 157 | if f then f() end |
158 | error(arg[2], 0) | 158 | error(arg[2], 0) |
159 | else | 159 | else |
160 | return unpack(arg) | 160 | return ... |
161 | end | 161 | end |
162 | end | 162 | end |
163 | end | 163 | end |