seqlua

view README @ 33:25cdfe854335

Changed one sentence in README
author jbe
date Sun Aug 24 00:26:34 2014 +0200 (2014-08-24)
parents 3ff7cec8d3ce
children 332216604f83
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(seq)`` to accept tables as well as functions (i.e function
7 iterators),
8 * add a new function ``string.concat(separator, seq)`` to concat either
9 table entries or function return values,
10 * provide auxiliary C functions and macros to simplify iterating over both
11 tables and iterator functions with the same statement.
13 Existing ``__ipairs`` or ``__index`` (but not ``__len``) metamethods are
14 respected by both the Lua functions and the C functions and macros. The
15 ``__ipairs`` metamethod takes precedence over ``__index``, while the
16 ``__len`` metamethod is never used.
20 Lua part of the library
21 -----------------------
23 The modified ``ipairs(seq)`` and the new ``string.concat(sep, seq)`` functions
24 accept either a table or a function as ``seq``. This is demonstrated in the
25 following examples:
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 print(string.concat(",", t))
40 -- prints: a,b,c
42 function alphabet()
43 local letter = nil
44 return function()
45 if letter == nil then
46 letter = "a"
47 elseif letter == "z" then
48 return nil
49 else
50 letter = string.char(string.byte(letter) + 1)
51 end
52 return letter
53 end
54 end
56 for i, v in ipairs(alphabet()) do
57 print(i, v)
58 end
59 -- prints:
60 -- 1 a
61 -- 2 b
62 -- 3 c
63 -- ...
64 -- 25 y
65 -- 26 z
67 print(string.concat(",", alphabet()))
68 -- prints: a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
70 function filter(f)
71 return function(seq)
72 return coroutine.wrap(function()
73 for i, v in ipairs(seq) do f(v) end
74 end)
75 end
76 end
78 alpha_beta_x = filter(function(v)
79 if v == "a" then
80 coroutine.yield("alpha")
81 elseif v == "b" then
82 coroutine.yield("beta")
83 elseif type(v) == "number" then
84 for i = 1, v do
85 coroutine.yield("X")
86 end
87 end
88 end)
90 print((","):concat(alpha_beta_x{"a", 3, "b", "c", "d"}))
91 -- prints: alpha,X,X,X,beta
93 print((","):concat(alpha_beta_x(alphabet())))
94 -- prints: alpha,beta
97 C part of the library
98 ---------------------
100 In ``seqlualib.h``, the following macro is defined:
102 #define seqlua_iterloop(L, iter, idx) \
103 for ( \
104 seqlua_iterinit((L), (iter), (idx)); \
105 seqlua_iternext(iter); \
106 )
108 and
110 #define seqlua_iterloopauto(L, iter, idx) \
111 for ( \
112 seqlua_iterinit((L), (iter), (idx)); \
113 seqlua_iternext(iter); \
114 lua_pop((L), 1) \
115 )
117 This macro allows iteration over either tables or iterator functions as the
118 following example function demonstrates:
120 int printcsv(lua_State *L) {
121 seqlua_Iterator iter;
122 seqlua_iterloop(L, &iter, 1) {
123 if (seqlua_itercount(&iter) > 1) fputs(",", stdout);
124 fputs(luaL_tolstring(L, -1, NULL), stdout);
125 // two values need to be popped (the value pushed by
126 // seqlua_iternext and the value pushed by luaL_tolstring)
127 lua_pop(L, 2);
128 }
129 fputs("\n", stdout);
130 return 0;
131 }
133 printcsv{"a", "b", "c"}
134 -- prints: a,b,c
136 printcsv(assert(io.open("testfile")):lines())
137 -- prints: line1,line2,... of "testfile"
139 NOTE: During iteration using ``seqlua_iterloop``, ``seqlua_iterloopauto``, or
140 ``seqlua_iterinit``, three extra elements are stored on the stack (additionally
141 to the value). These extra elements are removed automatically when the loop ends
142 (i.e. when ``seqlua_iternext`` returns zero). The value pushed onto the stack
143 for every iteration step has to be removed manually from the stack, unless
144 ``seqlua_iterloopauto`` is used.

Impressum / About Us