seqlua
view README @ 18:431fa6946a61
Precedence of __call and __index noted in README
author | jbe |
---|---|
date | Wed Aug 20 06:47:14 2014 +0200 (2014-08-20) |
parents | a95fbd16473f |
children | 2fad7f50076b |
line source
1 seqlua: Extended sequences and iterators in Lua
2 ===============================================
4 This is an experimental package to extend Lua in the following manner:
6 * allow ipairs(...) to accept tables as well as functions or iterator triplets,
7 * provide a function iterator(...) that returns single functions unmodified,
8 but converts
9 * iterator triplets into closures, and
10 * tables into a function closure that iterates over the elements,
11 * provide the auxiliary C functions and macros to simplify iterating over both
12 tables and iterator functions with the same statement.
14 This library completely ignores the ``__ipairs`` metamethod (as it is
15 deprecated since Lua 5.3.0-alpha). It respects, however, any ``__index`` and
16 ``__call`` metamethods (this may cause unexpected behavior when passing
17 callable tables to ``ipairs``). The ``__call`` metamethod takes precedence over
18 an existing ``__index`` metamethod.
22 Lua part of the library
23 -----------------------
25 The new ``ipairs(...)`` function works as follows:
27 require "seqlua"
29 t = {"a", "b", "c"}
31 for i, v in ipairs(t) do
32 print(i, v)
33 end
34 -- prints:
35 -- 1 a
36 -- 2 b
37 -- 3 c
39 function alphabet(from, to)
40 local letter = nil
41 return function()
42 if letter == nil then
43 letter = from
44 elseif letter == to then
45 return nil
46 else
47 letter = string.char(string.byte(letter) + 1)
48 end
49 return letter
50 end
51 end
53 f = alphabet("a", "z")
55 for i, v in ipairs(f) do
56 print(i, v)
57 end
58 -- prints:
59 -- 1 a
60 -- 2 b
61 -- 3 c
62 -- ...
63 -- 25 y
64 -- 26 z
66 c = setmetatable(
67 { iter = alphabet("a", "f") },
68 { __call = function(t) return t.iter() end }
69 )
71 for i, v in ipairs(c) do
72 print(i, v)
73 end
74 -- prints:
75 -- 1 a
76 -- 2 b
77 -- 3 c
78 -- 4 d
79 -- 5 e
80 -- 6 f
82 g = coroutine.wrap(function()
83 coroutine.yield("Alice")
84 coroutine.yield("Bob")
85 for i = 1, 3 do
86 coroutine.yield("Person #" .. tostring(i))
87 end
88 end)
90 for i, v in ipairs(g) do
91 print(i, v)
92 end
93 -- prints:
94 -- 1 Alice
95 -- 2 Bob
96 -- 3 Person #1
97 -- 4 Person #2
98 -- 5 Person #3
100 set = {apple = true, banana = true}
101 for i, k, v in ipairs(pairs(set)) do
102 print(i, k, v)
103 end
104 -- prints:
105 -- 1 banana true
106 -- 2 apple true
107 -- (order of "apple" and "banana" may vary)
109 The function ``iterator(...)`` may be used to convert any table, any function,
110 or any iterator triplet into a single function (possibly creating a closure):
112 require "seqlua"
114 function filter_strings(...)
115 nextvalue = iterator(...)
116 return function()
117 local value
118 repeat
119 value = nextvalue()
120 until value == nil or type(value) == "string"
121 return value
122 end
123 end
125 for i, v in ipairs(filter_strings{"Hello", true, "World"}) do
126 print(i, v)
127 end
128 -- prints:
129 -- 1 Hello
130 -- 2 World
132 tbl = {apple = true, banana = true, [1] = "array entry"}
133 for v in filter_strings(pairs(tbl)) do
134 print(v)
135 end
136 -- prints:
137 -- banana
138 -- apple
139 -- (order may vary)
143 C part of the library
144 ---------------------
146 In ``seqlualib.h``, the following macro is defined:
148 #define seqlua_iterloop(L, iter, idx) \
149 for ( \
150 seqlua_iterinit((L), (iter), (idx)); \
151 seqlua_iternext(iter); \
152 lua_pop((L), 1) \
153 )
155 This macro allows iteration over either tables or iterator functions (but not
156 iterator triplets) as the following example function demonstrates:
158 int printcsv(lua_State *L) {
159 seqlua_Iterator iter;
160 seqlua_iterloop(L, &iter, 1) {
161 if (seqlua_itercount(&iter) > 1) fputs(",", stdout);
162 fputs(luaL_tolstring(L, -1, NULL), stdout);
163 lua_pop(L, 1); // pops value that luaL_tolstring pushed onto stack
164 }
165 fputs("\n", stdout);
166 return 0;
167 }
169 printcsv{"a", "b", "c"}
170 -- prints: a,b,c
172 printcsv(assert(io.open("testfile")):lines())
173 -- prints: line1,line2,... of "testfile"
175 Additionally, ``seqlualib`` includes a function ``seqlua_iterclosure(L, idx)``,
176 which converts a table at a given stack index into a function closure (stored
177 on the same stack index) that iterates over the elements of the table. If the
178 value at the given stack index is already a function (or if it is callable
179 through a ``__call`` metamethod), then ``seqlua_iterclosure(L, idx)`` leaves
180 the value at ``idx`` unchanged.