seqlua

view README @ 28:3287510a35e3

Modified filter example in README (shows effect on alphabet() too)
author jbe
date Fri Aug 22 12:25:57 2014 +0200 (2014-08-22)
parents 18292ffaf2ac
children 2c4d9e44f704
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,
7 * add a new function ``string.concat(separator, seq)`` to concat either table
8 entries or function return values,
9 * provide auxiliary C functions and macros to simplify iterating over both
10 tables and iterator functions with the same statement.
12 In other words:
13 When calling ``ipairs(seq)`` or ``string.concat(separator, seq)``,
14 then ``seq`` may either be a (table) sequence or a (function) iterator.
15 Auxiliary C functions and macros are provided to simplify extending your own
16 C functions in the same manner.
18 This library completely ignores the ``__ipairs`` metamethod (as it is
19 deprecated since Lua 5.3.0-alpha). It respects, however, any ``__index`` and
20 ``__call`` metamethods (this may cause unexpected behavior when passing
21 callable tables to ``ipairs``). The ``__call`` metamethod takes precedence over
22 an existing ``__index`` metamethod.
26 Lua part of the library
27 -----------------------
29 The modified ``ipairs(seq)`` function and the new ``string.concat`` function
30 work as demonstrated in the following examples:
32 require "seqlua"
34 t = {"a", "b", "c"}
36 for i, v in ipairs(t) do
37 print(i, v)
38 end
39 -- prints:
40 -- 1 a
41 -- 2 b
42 -- 3 c
44 print(string.concat(",", t))
45 -- prints: a,b,c
47 function alphabet()
48 local letter = nil
49 return function()
50 if letter == nil then
51 letter = "a"
52 elseif letter == "z" then
53 return nil
54 else
55 letter = string.char(string.byte(letter) + 1)
56 end
57 return letter
58 end
59 end
61 for i, v in ipairs(alphabet()) do
62 print(i, v)
63 end
64 -- prints:
65 -- 1 a
66 -- 2 b
67 -- 3 c
68 -- ...
69 -- 25 y
70 -- 26 z
72 print(string.concat(",", alphabet()))
73 -- 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
75 function filter(f)
76 return function(seq)
77 return coroutine.wrap(function()
78 for i, v in ipairs(seq) do f(v) end
79 end)
80 end
81 end
83 number_to_trues = filter(function(v)
84 if v == "a" then
85 coroutine.yield("alpha")
86 elseif v == "b" then
87 coroutine.yield("beta")
88 elseif type(v) == "number" then
89 for i = 1, v do
90 coroutine.yield("X")
91 end
92 end
93 end)
95 print((","):concat(number_to_trues{"a", 3, "b", "c", "d"}))
96 -- prints: alpha,X,X,X,beta
98 print((","):concat(number_to_trues(alphabet())))
99 -- prints: alpha,beta
102 C part of the library
103 ---------------------
105 In ``seqlualib.h``, the following macro is defined:
107 #define seqlua_iterloop(L, iter, idx) \
108 for ( \
109 seqlua_iterinit((L), (iter), (idx)); \
110 seqlua_iternext(iter); \
111 )
113 and
115 #define seqlua_iterloopauto(L, iter, idx) \
116 for ( \
117 seqlua_iterinit((L), (iter), (idx)); \
118 seqlua_iternext(iter); \
119 lua_pop((L), 1) \
120 )
122 This macro allows iteration over either tables or iterator functions as the
123 following example function demonstrates:
125 int printcsv(lua_State *L) {
126 seqlua_Iterator iter;
127 seqlua_iterloop(L, &iter, 1) {
128 if (seqlua_itercount(&iter) > 1) fputs(",", stdout);
129 fputs(luaL_tolstring(L, -1, NULL), stdout);
130 // two values need to be popped (the value pushed by
131 // seqlua_iternext and the value pushed by luaL_tolstring)
132 lua_pop(L, 2);
133 }
134 fputs("\n", stdout);
135 return 0;
136 }
138 printcsv{"a", "b", "c"}
139 -- prints: a,b,c
141 printcsv(assert(io.open("testfile")):lines())
142 -- prints: line1,line2,... of "testfile"

Impressum / About Us