seqlua

view README @ 4:44eddb9cea1e

Fixed mistake in README: seqlua_iterclosure(L, idx) respects __call, not __ipairs
author jbe
date Wed Aug 20 01:44:13 2014 +0200 (2014-08-20)
parents 22ec338adc20
children e29e2d7b73a5
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 ``__call``
16 metamethods (this may cause unexpected behavior when passing callable tables
17 to ``ipairs``).
21 Lua part of the library
22 -----------------------
24 The new ``ipairs(...)`` function works as follows:
26 require "seqlua"
28 t = {"a", "b", "c"}
30 for i, v in ipairs(t) do
31 print(i, v)
32 end
33 -- prints:
34 -- 1 a
35 -- 2 b
36 -- 3 c
38 function alphabet()
39 local letter = nil
40 return function()
41 if letter == nil then
42 letter = "a"
43 elseif letter == "z" then
44 return nil
45 else
46 letter = string.char(string.byte(letter) + 1)
47 end
48 return letter
49 end
50 end
52 f = alphabet()
54 for i, v in ipairs(f) do
55 print(i, v)
56 end
57 -- prints:
58 -- 1 a
59 -- 2 b
60 -- 3 c
61 -- ...
62 -- 25 y
63 -- 26 z
65 set = {apple = true, banana = true}
66 for i, k, v in ipairs(pairs(set)) do
67 print(i, k, v)
68 end
69 -- prints:
70 -- 1 banana true
71 -- 2 apple true
72 -- (order of "apple" and "banana" may vary)
74 The function ``iterator(...)`` may be used to convert any table, any function,
75 or any iterator triplet into a single function (possibly creating a closure):
77 require "seqlua"
79 function filter_strings(...)
80 nextvalue = iterator(...)
81 return function()
82 local value
83 repeat
84 value = nextvalue()
85 until value == nil or type(value) == "string"
86 return value
87 end
88 end
90 for i, v in ipairs(filter_strings{"Hello", true, "World"}) do
91 print(i, v)
92 end
93 -- prints:
94 -- 1 Hello
95 -- 2 World
97 tbl = {apple = true, banana = true, [1] = "array entry"}
98 for v in filter_strings(pairs(tbl)) do
99 print(v)
100 end
101 -- prints:
102 -- banana
103 -- apple
104 -- (order may vary)
108 C part of the library
109 ---------------------
111 In ``seqlualib.h``, the following macro is defined:
113 #define seqlua_iterloop(L, iter, idx) \
114 for ( \
115 seqlua_iterinit((L), (iter), (idx)); \
116 seqlua_iternext(iter); \
117 lua_pop((L), 1) \
118 )
120 This macro allows iteration over either tables or iterator functions (but not
121 iterator triplets) as the following example function demonstrates:
123 int printcsv(lua_State *L) {
124 seqlua_Iterator iter;
125 seqlua_iterloop(L, &iter, 1) {
126 if (seqlua_itercount(&iter) > 1) fputs(",", stdout);
127 fputs(luaL_tolstring(L, -1, NULL), stdout);
128 lua_pop(L, 1); // pops value that luaL_tolstring pushed onto stack
129 }
130 fputs("\n", stdout);
131 return 0;
132 }
134 printcsv{"a", "b", "c"} -- prints: a,b,c
136 NOTE: ``seqlua_iterinit`` will store one extra element on the stack during
137 iteration. When ``seqlua_iternext`` returns 0, this extra element is popped
138 from the stack automatically.
140 Additionally, ``seqlualib`` includes a function ``seqlua_iterclosure(L, idx)``,
141 which converts a table at a given stack index into a function closure (stored
142 on the same stack index) that iterates over the elements of the table. If the
143 value at the given stack index is already a function, it leaves the value
144 unchanged. If the value is convertible to a function using ``__call,`` then the
145 function is replaced by its ``__call`` metamethod.

Impressum / About Us