seqlua
view README @ 12:91b0d0d4c4dd
Added coroutine example to README
| author | jbe | 
|---|---|
| date | Wed Aug 20 05:18:16 2014 +0200 (2014-08-20) | 
| parents | 814d42156b14 | 
| children | 2ef6eb7d17d2 | 
 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(from, to)
    39       local letter = nil
    40       return function()
    41         if letter == nil then
    42           letter = from
    43         elseif letter == to 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("a", "z")
    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     c = setmetatable(
    66       { iter = alphabet("a", "f") },
    67       { __call = function(t) return t.iter() end }
    68     )
    70     for i, v in ipairs(c) do
    71       print(i, v)
    72     end
    73     -- prints:
    74     --  1   a
    75     --  2   b
    76     --  3   c
    77     --  4   d
    78     --  5   e
    79     --  6   f
    81     g = function()
    82       coroutine.yield("Alice")
    83       coroutine.yield("Bob")
    84       for i = 1, 3 do
    85         coroutine.yield("Person #" .. tostring(i))
    86       end
    87     end
    88     h = coroutine.wrap(g)
    90     for i, v in ipairs(h) 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 NOTE: ``seqlua_iterinit`` will store one extra element on the stack during
   176 iteration. When ``seqlua_iternext`` returns 0, this extra element is popped
   177 from the stack automatically.
   179 Additionally, ``seqlualib`` includes a function ``seqlua_iterclosure(L, idx)``,
   180 which converts a table at a given stack index into a function closure (stored
   181 on the same stack index) that iterates over the elements of the table. If the
   182 value at the given stack index is already a function, it leaves the value
   183 unchanged. If the value is convertible to a function using ``__call,`` then the
   184 value is replaced by a closure calling the ``__call`` metamethod.
