seqlua
diff README @ 23:29792283522f
Removed iterator(...) function; ipairs doesn't accept iterator triplets anymore
author | jbe |
---|---|
date | Thu Aug 21 20:01:52 2014 +0200 (2014-08-21) |
parents | 50c6388d4963 |
children | 44880bcfc323 |
line diff
1.1 --- a/README Thu Aug 21 13:04:45 2014 +0200 1.2 +++ b/README Thu Aug 21 20:01:52 2014 +0200 1.3 @@ -3,12 +3,8 @@ 1.4 1.5 This is an experimental package to extend Lua in the following manner: 1.6 1.7 -* allow ipairs(...) to accept tables as well as functions or iterator triplets, 1.8 -* provide a function iterator(...) that returns single functions unmodified, 1.9 - but converts 1.10 - * iterator triplets into closures, and 1.11 - * tables into a function closure that iterates over the elements, 1.12 -* provide the auxiliary C functions and macros to simplify iterating over both 1.13 +* allow ipairs(...) to accept tables as well as functions 1.14 +* provide auxiliary C functions and macros to simplify iterating over both 1.15 tables and iterator functions with the same statement. 1.16 1.17 This library completely ignores the ``__ipairs`` metamethod (as it is 1.18 @@ -50,9 +46,7 @@ 1.19 end 1.20 end 1.21 1.22 - f = alphabet("a", "z") 1.23 - 1.24 - for i, v in ipairs(f) do 1.25 + for i, v in ipairs(alphabet()) do 1.26 print(i, v) 1.27 end 1.28 -- prints: 1.29 @@ -63,50 +57,33 @@ 1.30 -- 25 y 1.31 -- 26 z 1.32 1.33 - set = {apple = true, banana = true} 1.34 - for i, k, v in ipairs(pairs(set)) do 1.35 - print(i, k, v) 1.36 + function filter(f, seq) 1.37 + return coroutine.wrap(function() 1.38 + for i, v in ipairs(seq) do f(v) end 1.39 + end) 1.40 end 1.41 - -- prints: 1.42 - -- 1 banana true 1.43 - -- 2 apple true 1.44 - -- (order of "apple" and "banana" may vary) 1.45 - 1.46 -More examples for invoking the ``ipairs(...)`` function can be found in the 1.47 -file ``seqlua_ipairs_example.lua``. 1.48 1.49 -The function ``iterator(...)`` may be used to convert any table, any function, 1.50 -or any iterator triplet into a single function (possibly creating a closure): 1.51 - 1.52 - require "seqlua" 1.53 - 1.54 - function filter_strings(...) 1.55 - nextvalue = iterator(...) 1.56 - return function() 1.57 - local value 1.58 - repeat 1.59 - value = nextvalue() 1.60 - until value == nil or type(value) == "string" 1.61 - return value 1.62 + function filterfunc(v) 1.63 + local type_v = type(v) 1.64 + if type_v == "string" then 1.65 + coroutine.yield(v) 1.66 + elseif type_v == "number" then 1.67 + for i = 1, v do 1.68 + coroutine.yield(true) 1.69 + end 1.70 end 1.71 end 1.72 1.73 - for i, v in ipairs(filter_strings{"Hello", true, "World"}) do 1.74 - print(i, v) 1.75 - end 1.76 - -- prints: 1.77 - -- 1 Hello 1.78 - -- 2 World 1.79 - 1.80 - tbl = {apple = true, banana = true, [1] = "array entry"} 1.81 - for v in filter_strings(pairs(tbl)) do 1.82 + for v in filter(filterfunc, {"a", "b", 3, "c"}) do 1.83 print(v) 1.84 end 1.85 -- prints: 1.86 - -- banana 1.87 - -- apple 1.88 - -- (order may vary) 1.89 - 1.90 + -- a 1.91 + -- b 1.92 + -- true 1.93 + -- true 1.94 + -- true 1.95 + -- c 1.96 1.97 1.98 C part of the library 1.99 @@ -121,8 +98,8 @@ 1.100 lua_pop((L), 1) \ 1.101 ) 1.102 1.103 -This macro allows iteration over either tables or iterator functions (but not 1.104 -iterator triplets) as the following example function demonstrates: 1.105 +This macro allows iteration over either tables or iterator functions as the 1.106 +following example function demonstrates: 1.107 1.108 int printcsv(lua_State *L) { 1.109 seqlua_Iterator iter; 1.110 @@ -141,20 +118,4 @@ 1.111 printcsv(assert(io.open("testfile")):lines()) 1.112 -- prints: line1,line2,... of "testfile" 1.113 1.114 -Additionally, ``seqlualib`` includes a function ``seqlua_iterclosure(L, idx)``, 1.115 -which converts a table at a given stack index into a function closure (stored 1.116 -on the same stack index) that iterates over the elements of the table. If the 1.117 -value at the given stack index is already a function (or if it is callable 1.118 -through a ``__call`` metamethod), then ``seqlua_iterclosure(L, idx)`` leaves 1.119 -the value at ``idx`` unchanged. 1.120 1.121 - 1.122 -TODO 1.123 ----- 1.124 - 1.125 -Accepting iterator triplets doesn't work, because even if a single function 1.126 -is passed to ipairs, that function still might expect to get the previous 1.127 -iteration value as second argument. Therefore, accepting iterator triplets 1.128 -as argument to ipairs should be removed. 1.129 - 1.130 -