seqlua
diff README @ 52:3362ec36cb09
Do not automatically assume that functions passed to ipairs are iterators
but require ipairs(func, mode) to have an explicit mode set to "call" or "generator"
but require ipairs(func, mode) to have an explicit mode set to "call" or "generator"
author | jbe |
---|---|
date | Tue Aug 26 21:10:03 2014 +0200 (2014-08-26) |
parents | 06c5f2f9ec41 |
children | 664736a8fcbf |
line diff
1.1 --- a/README Mon Aug 25 12:12:46 2014 +0200 1.2 +++ b/README Tue Aug 26 21:10:03 2014 +0200 1.3 @@ -1,11 +1,15 @@ 1.4 seqlua: Extension for handling sequential data in Lua 1.5 ===================================================== 1.6 1.7 -This package is an extension for the Lua programming language (version 5.2) 1.8 -which: 1.9 +This package is an experimental extension for the Lua programming language 1.10 +(version 5.2) which: 1.11 1.12 -* allows ``ipairs(seq)`` to accept either tables or functions (i.e function 1.13 - iterators) as an argument, 1.14 +* makes ``ipairs(tbl)`` respect both metamethods ``__index`` and ``__ipairs`` 1.15 + (where ``__ipairs`` has precedence over ``__index``), 1.16 +* allows ``ipairs(seq, "call")`` to accept either tables or functions as first 1.17 + argument where a function is used as iterator, 1.18 +* allows ``ipairs(seq, "generator")`` to accept either tables or functions as 1.19 + first argument where a function is used as generator for an iterator, 1.20 * adds a new function ``string.concat(separator, seq)`` that concats either 1.21 table entries or function return values, 1.22 * provides auxiliary C functions and macros to simplify iterating over both 1.23 @@ -94,8 +98,15 @@ 1.24 1.25 This extension, however, modifies Lua's ``ipairs`` statement in such way that 1.26 it automatically accepts either a table or an iterator function as argument. 1.27 -Thus, the first of the three ``write_lines`` functions above will accept both 1.28 -(table) sequences and (function) iterators. 1.29 +Thus, the function below will accept both (table) sequences and (function) 1.30 +iterators: 1.31 + 1.32 + function write_lines(lines) 1.33 + for i, line in ipairs(lines, "call") do 1.34 + io.stdout:write(line) 1.35 + io.stdout:write("\n") 1.36 + end 1.37 + end 1.38 1.39 In addition to the modification of ``ipairs``, it also provides C functions and 1.40 macros to iterate over values in the same manner as a generic loop statement 1.41 @@ -167,7 +178,7 @@ 1.42 1.43 t = {"a", "b", "c"} 1.44 1.45 - for i, v in ipairs(t) do 1.46 + for i, v in ipairs(t, "call") do 1.47 print(i, v) 1.48 end 1.49 -- prints: 1.50 @@ -192,7 +203,18 @@ 1.51 end 1.52 end 1.53 1.54 - for i, v in ipairs(alphabet()) do 1.55 + for i, v in ipairs(alphabet(), "call") do 1.56 + print(i, v) 1.57 + end 1.58 + -- prints: 1.59 + -- 1 a 1.60 + -- 2 b 1.61 + -- 3 c 1.62 + -- ... 1.63 + -- 25 y 1.64 + -- 26 z 1.65 + 1.66 + for i, v in ipairs(alphabet, "generator") do 1.67 print(i, v) 1.68 end 1.69 -- prints: 1.70 @@ -209,7 +231,7 @@ 1.71 function filter(f) 1.72 return function(seq) 1.73 return coroutine.wrap(function() 1.74 - for i, v in ipairs(seq) do f(v) end 1.75 + for i, v in ipairs(seq, "call") do f(v) end 1.76 end) 1.77 end 1.78 end