seqlua
view README @ 24:28d472cc7a9b
Removed seqlua_iterclosure header in seqlualib.h
| author | jbe | 
|---|---|
| date | Thu Aug 21 20:20:20 2014 +0200 (2014-08-21) | 
| parents | 29792283522f | 
| children | 44880bcfc323 | 
 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
     7 * provide auxiliary C functions and macros to simplify iterating over both
     8   tables and iterator functions with the same statement.
    10 This library completely ignores the ``__ipairs`` metamethod (as it is
    11 deprecated since Lua 5.3.0-alpha). It respects, however, any ``__index`` and
    12 ``__call`` metamethods (this may cause unexpected behavior when passing
    13 callable tables to ``ipairs``). The ``__call`` metamethod takes precedence over
    14 an existing ``__index`` metamethod.
    18 Lua part of the library
    19 -----------------------
    21 The new ``ipairs(...)`` function works as follows:
    23     require "seqlua"
    25     t = {"a", "b", "c"}
    27     for i, v in ipairs(t) do
    28       print(i, v)
    29     end
    30     -- prints:
    31     --  1   a
    32     --  2   b
    33     --  3   c
    35     function alphabet()
    36       local letter = nil
    37       return function()
    38         if letter == nil then
    39           letter = "a"
    40         elseif letter == "z" then
    41           return nil
    42         else
    43           letter = string.char(string.byte(letter) + 1)
    44         end
    45         return letter
    46       end
    47     end
    49     for i, v in ipairs(alphabet()) do
    50       print(i, v)
    51     end
    52     -- prints:
    53     --  1   a
    54     --  2   b
    55     --  3   c
    56     --  ...
    57     --  25  y
    58     --  26  z
    60     function filter(f, seq)
    61       return coroutine.wrap(function()
    62         for i, v in ipairs(seq) do f(v) end
    63       end)
    64     end
    66     function filterfunc(v)
    67       local type_v = type(v)
    68       if type_v == "string" then
    69         coroutine.yield(v)
    70       elseif type_v == "number" then
    71         for i = 1, v do
    72           coroutine.yield(true)
    73         end
    74       end
    75     end
    77     for v in filter(filterfunc, {"a", "b", 3, "c"}) do
    78       print(v)
    79     end
    80     -- prints:
    81     --  a
    82     --  b
    83     --  true
    84     --  true
    85     --  true
    86     --  c
    89 C part of the library
    90 ---------------------
    92 In ``seqlualib.h``, the following macro is defined:
    94     #define seqlua_iterloop(L, iter, idx) \
    95       for ( \
    96         seqlua_iterinit((L), (iter), (idx)); \
    97         seqlua_iternext(iter); \
    98         lua_pop((L), 1) \
    99       )
   101 This macro allows iteration over either tables or iterator functions as the
   102 following example function demonstrates:
   104     int printcsv(lua_State *L) {
   105       seqlua_Iterator iter;
   106       seqlua_iterloop(L, &iter, 1) {
   107         if (seqlua_itercount(&iter) > 1) fputs(",", stdout);
   108         fputs(luaL_tolstring(L, -1, NULL), stdout);
   109         lua_pop(L, 1);  // pops value that luaL_tolstring pushed onto stack
   110       }
   111       fputs("\n", stdout);
   112       return 0;
   113     }
   115     printcsv{"a", "b", "c"}
   116     -- prints: a,b,c
   118     printcsv(assert(io.open("testfile")):lines())
   119     -- prints: line1,line2,... of "testfile"
